# spawn(函数,参数...) 启动一个协成
# join() 阻塞,直到某个协程执行完毕
# joinall 类似于join 只不过
g1.join()
g2.join()
gevent.joinall([g1,g2]) 一次性把所有需要阻塞的协程对象写到一起
# value 获取协成的返回值
import time, gevent def eat(): print("eating 111") time.sleep(1) print("eating 222") return "吃完了" def play(): print("play 111") time.sleep(1) print("play 222") return "play done" g1 = gevent.spawn(eat) g2 = gevent.spawn(play) gevent.joinall([g1, g2]) print(g1.value) print(g2.value)
执行结果:
eating 222 play 111 play 222 吃完了 play done