赞
踩
同步、异步
阻塞、非阻塞
启动生成器: g.send(None) 或者 next(g), 如果不调用next 第一次必须send(None)
关闭生成器: close()
def create_num(num): a, b = 0, 1 current_num = 0 while current_num < num: res = yield a print('res-->', res) a, b = b, a+b current_num += 1 # return 'yxh' g = create_num(5) # 启动生成器 g.send(None) 或者 next(g), 如果不调用next 第一次必须send(None) print(next(g)) print(g.send('yxh')) print(g.send('sdad')) # 关闭生成器 g.close() """ 结果: 0 res--> yxh 1 res--> sdad 1 """
import time def task1(): while True: print('--1--') # time.sleep(0.1) yield def task2(): while True: print('--2--') # time.sleep(0.1) yield def main(): t1 = task1() t2 = task2() while True: next(t1) next(t2) if __name__ == '__main__': main()
yield from介绍
python3.3新加了yield from语法
def generator_1(): total = 0 while True: x = yield print('加', x) if not x: break total += x return total def generator_2(): # 委托生成器 while True: total = yield from generator_1() # 子生成器 print('加和总数是:', total) def main(): # 调用方 # g1 = generator_1() # g1.send(None) # g1.send(2) # g1.send(3) # g1.send(None) g2 = generator_2() g2.send(None) g2.send(2) g2.send(3) g2.send(None) if __name__ == '__main__': main()
结果:加 2
加 3
加 None
加和总数是: 5
【子生成器】:yield from后的generator_1()生成器函数是子生成器
【委托生成器】:generator_2()是程序中的委托生成器,它负责委托子生成器完成具体任务。
【调用方】:main()是程序中的调用方,负责调用委托生成器。
from greenlet import greenlet import time # 协程利用程序的IO 来切换任务 def demo1(): while True: print('demo1') gr2.switch() time.sleep(0.5) def demo2(): while True: print('demo2') gr1.switch() time.sleep(0.5) gr1 = greenlet(demo1) gr2 = greenlet(demo2) gr1.switch() gr2.switch()
import gevent import time from gevent import monkey # 将程序中用到的耗时操作 换为gevent中实现的模块 monkey.patch_all() """ gevent.getcurrent() 获取当前gevent对象 代码重构 可以 from gevent import monkey 的 monkey.patch_all() """ def f1(n): for i in range(n): print(gevent.getcurrent(), i) time.sleep(0.5) # gevent.sleep(0.5) def f2(n): for i in range(n): print(gevent.getcurrent(), i) time.sleep(0.5) # gevent.sleep(0.5) def f3(n): for i in range(n): print(gevent.getcurrent(), i) time.sleep(0.5) # gevent.sleep(0.5) print('--1--') g1 = gevent.spawn(f1, 5) print('--2--') time.sleep(1) # gevent.sleep(0.5) g2 = gevent.spawn(f2, 5) print('--3--') g3 = gevent.spawn(f3, 5) print('--4--') g1.join() g2.join() g3.join()
import gevent from gevent import monkey monkey.patch_all() # requests放在 monkey.patch_all()后面导入不会有报警 import requests # urllib 进行封装 def download(url): print('get:%s' % url) res = requests.get(url) data = res.text print(len(data), url) g1 = gevent.spawn(download, 'https://www.baidu.com/') g2 = gevent.spawn(download, 'https://www.python.org/') g3 = gevent.spawn(download, 'https://www.baidu.com/') g1.join() g2.join() g3.join() gevent.joinall([ gevent.spawn(download, 'https://www.baidu.com/'), gevent.spawn(download, 'https://www.python.org/'), gevent.spawn(download, 'https://www.baidu.com/') ]) """ 结果 get:https://www.baidu.com/ get:https://www.python.org/ get:https://www.baidu.com/ 2443 https://www.baidu.com/ 2443 https://www.baidu.com/ 48794 https://www.python.org/ get:https://www.baidu.com/ get:https://www.python.org/ get:https://www.baidu.com/ 2443 https://www.baidu.com/ 2443 https://www.baidu.com/ 48794 https://www.python.org/ """
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。