python gevent异步

安装

pip install gevent
import gevent
from gevent import monkey
monkey.patch_all()#捕捉所有阻塞,不止接收gevent.sleep
import time
def f(s):
    print("hello-----%s"%s)
    time.sleep(3)
def f2(s):
    time.sleep(3)
    print("hello------%s"%s)
start=time.time()
t=gevent.spawn(f,"f")
t1=gevent.spawn(f2,"f2")
gevent.joinall([t,t1])
end=time.time()
print(end-start)
  hello-----f  hello------f2  3.003866195678711

创建协程对象

gevent.spawn(函数,参数)

执行协程并阻塞等待执行完毕

gevent.joinall([协程对象],[timeout])
 

相关推荐