协程,利用线程在等待其他资源期间去执行其他的函数。
gevent里面封装了greenlet,greenlet里面封装了yield。
from gevent import monkey
import gevent
import time # 有耗时操作时需要
monkey.patch_all() def test(name):
for i in range(5):
print(name,i)
time.sleep(0.5) gevent.joinall([
gevent.spawn(test,"work1"),
gevent.spawn(test,"work2")
])
输出
work1 0
work2 0
work1 1
work2 1
work1 2
work2 2
work1 3
work2 3
work1 4
work2 4