python gevent异步

别说谁变了你拦得住时间么 提交于 2019-12-07 01:05:33

安装

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])

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!