1.迭代器程序(实现菲比那次数列并且可以抛出与接收异常)

def fib(max):
n,a,b = 0,0,1
while n < max:
#print(b)
yield b
a,b = b,a+b
n= n+1
return 'done'
g = fib(10)
while True:
try:
x = g.__next__()
print('g:',x)
except StopIteration as e:
print('Generator return value:',e.value)
break

2.利用迭代器实现简单的“单线程并行”吃包子(实际上还是串行)

import time
def consumer(name):
print('[%s] prepare to eat baozi!!'%name)
while True:
baozi = yield
print('baozi [%s] is coming,ate by [%s]'%(baozi,name)) def producer(name):
c = consumer('A')
c2 = consumer('B')
c.__next__()
c2.__next__()
print('start'.center(30,'-'))
for i in range(10):
time.sleep(1)
print('just one,half to half')
c.send(i)
c2.send(i) producer('wt')

  

最新文章

  1. 在Oracle中使用Entity Framework 6 CodeFirst
  2. 数据库递归查询-CTE
  3. IntelliJ IDEA 的 Java 热部署插件 JRebel 安装及使用
  4. Jordan Lecture Note-3: 梯度投影法
  5. java模拟http post
  6. 一个很奇特的异常 tmpFile.renameTo(classFile) failed
  7. sun.proxy.$Proxy0 cannot be cast to dao.impl.MyAdviceImpl
  8. Webstrom 常用操作记录
  9. 错误:&#39;dict&#39; object is not callable
  10. 查看 Apache并发请求数及其TCP连接状态【转】
  11. Android OpenGL ES 开发(五): OpenGL ES 使用投影和相机视图
  12. LabView(控件部分)
  13. ReactiveCocoa(II)
  14. win10和ubuntu16.04双系统Geom Error
  15. vue的采坑之旅--vue-cli脚手架loader重复--Invalid CSS after &quot;...load the styles&quot;: expected 1 selector or at-rule
  16. JavaBean(web基础学习笔记十二)
  17. JAVA问题定位跟踪技术
  18. 生存分析/Weibull Distribution韦布尔分布
  19. c/c++中关于String类型的思考
  20. 数据结构--ADT

热门文章

  1. 7.Selenium+Python实现搜索百度的测试用例
  2. Oracle存储过程返回
  3. 使用内省的方式操作JavaBean
  4. 2015.9.28 不能将多个项传入“Microsoft.Build.Framework.ITaskItem”类型的参数 问题解决
  5. composer update的错误使用以及如何更新composer.lock文件
  6. 关于struts2.3的action
  7. .net 4 安装未成功,无意中的解决办法!
  8. spring-初始化bean
  9. Python操作远程机器
  10. Python之POST登录测试