【Python】gevent模块
from gevent import monkey; monkey.patch_all()
import gevent
import time
# 可实现切换,同时检测IO
def eat(name):
print('%s eat 1' % name)
time.sleep(3) # gevent 本身只支持geven.sleep, 想要支持time.sleep(),就要开头写上monkey
print('%s eat 2' % name)
time.sleep(4)
def play(name):
print('%s play 1' % name)
time.sleep(4)
print('%s play 2' % name)
time.sleep(4)
g1 = gevent.spawn(eat, 'caya')
g2 = gevent.spawn(play, 'caya')
g1.join()
g2.join()