1.gevent执行
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