import gevent
from gevent import monkey
import time
import 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()
'''