python协程io自动切换--gevent
1.gevent执行
import geventdef 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 startfunc2 startfunc3 startfunc3 endfunc2 endfunc1 end
2.gevent 中的mokey
from urllib import requestimport geventimport timefrom gevent import monkeymonkey.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.orghttps://www.python.org地址的字节数为:48834爬取地址:https://github.comhttps://github.com地址的字节数为:86520同步执行时间为: 7.055272817611694爬取地址:http://www.sina.com.cn/爬取地址:https://www.python.org爬取地址:https://github.comhttp://www.sina.com.cn/地址的字节数为:570200https://www.python.org地址的字节数为:48834https://github.com地址的字节数为:59615异步执行时间为: 2.019655466079712
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。