python协程io自动切换--gevent
2018-07-11 14:17
573 查看
1.gevent执行
2.gevent 中的mokey
import gevent def func1(): print('func1 start') gevent.sleep(2) print('func1 end') def func2(): print('func2 start') gevent.sleep(1) print('func2 end') def func3(): print('func3 start') gevent.sleep(0) print('func3 end') if __name__=='__main__': gevent.joinall([ gevent.spawn(func1), gevent.spawn(func2), gevent.spawn(func3), ]) >>: func1 start func2 start func3 start func3 end func2 end func1 end
2.gevent 中的mokey
from urllib import request import gevent import time from gevent import monkey monkey.patch_all()#把所有的io操作做上标记 def func(url): print('爬取地址:%s'%url) resp=request.urlopen(url) data=resp.read() print('%s地址的字节数为:%s'%(url,len(data))) urls=['http://www.sina.com.cn/', 'https://www.python.org', 'https://github.com', ] if __name__=='__main__': start_time=time.time() for url in urls: func(url) print("同步执行时间为:",time.time()-start_time) asvnc_start_time=time.time() gevent.joinall([ gevent.spawn(func,'http://www.sina.com.cn/'), gevent.spawn(func,'https://www.python.org'), gevent.spawn(func,'https://github.com') ]) print("异步执行时间为:", time.time() - asvnc_start_time) >>: 爬取地址:http://www.sina.com.cn/ http://www.sina.com.cn/地址的字节数为:570200 爬取地址:https://www.python.org https://www.python.org地址的字节数为:48834 爬取地址:https://github.com https://github.com地址的字节数为:86520 同步执行时间为: 7.055272817611694 爬取地址:http://www.sina.com.cn/ 爬取地址:https://www.python.org 爬取地址:https://github.com http://www.sina.com.cn/地址的字节数为:570200 https://www.python.org地址的字节数为:48834 https://github.com地址的字节数为:59615 异步执行时间为: 2.019655466079712
相关文章推荐
- python自动代理切换
- Python 并发编程之协程/异步IO
- python中的协程模块gevent
- Python 中进程、线程、协程、IO复用
- Gevent-socketio文档-Socket.IO的一个python实现
- Python 通过gevent实现协程
- python 协程与gevent
- python3通过gevent.pool限制协程并发数量
- python中的协程:greenlet和gevent
- 自己动手写Python实现Ubuntu自动切换壁纸
- Python学习:异步IO:协程和asyncio
- 协程-遇到I/O自动切换
- Python运维之路——协程、事件驱动与异步IO
- Windows上利用Python自动切换代理IP的终极方案!
- 异步IO:Python中的协程
- 【python】gevent协程例子
- Python并发编程协程(Coroutine)之Gevent
- python--gevent协程及协程概念
- python 脚本自动切换windows代理设置