编程语言
首页 > 编程语言> > python-协程gevent的使用

python-协程gevent的使用

作者:互联网

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()
'''

标签:spawn,work,协程,python,random,gevent,import,work2
来源: https://www.cnblogs.com/fuyouqiang/p/11775772.html