python-协程gevent的使用

import geventfrom gevent import monkeyimport timeimport random# 有耗时操作时需要monkey.patch_all()  # 将程序中用到的耗时操作代码,换为 gevent 中自己实现的模块def work(name):    for i in range(10):        print(name, i)        time.sleep(random.random())gevent.joinall([    gevent.spawn(work, ‘work1‘),    gevent.spawn(work, ‘work2‘)])# 等价于‘‘‘g1 = gevent.spawn(work, ‘work1‘)g2 = gevent.spawn(work, ‘work2‘)g1.join()g2.join()‘‘‘

相关推荐