标签:auth odi 爬虫 monkey 模块 env lock mon lis
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# Date: 2019/11/30
# Author: SunXiuWen
"""pip install -i https://pypi.douban.com/simple/ gevent"""
import time
import gevent
from gevent import monkey
monkey.patch_all()
def task(i):
time.sleep(3)
print(i)
def work():
g_list = [gevent.spawn(task, i) for i in range(10)]
gevent.joinall(g_list)
return [i.value for i in g_list]
if __name__ == '__main__':
start = time.time()
ret = work()
end = time.time() - start
print(ret, '\n', end)
"""结论:
如果不配置monkey,则是阻塞状态,要花费30秒左右,且导入gevent模块必须在一个模块中
"""
工作应用:如爬虫,下载是一个函数f1,解析是一个函数f2,都通过spawn住,然后通过
gevent.joinall([gevent.spawn(f1),gevent.spawn(f2)])
从而实现协程并发!
标签:auth odi 爬虫 monkey 模块 env lock mon lis
原文地址:https://www.cnblogs.com/sunxiuwen/p/12234340.html