python 同步与异步的性能区别以及遇到IO阻塞时会自动切换任务

 


#网页爬取
import urllib,time,gevent
import urllib.request
from gevent import monkey

monkey.patch_all()#把当前程序的所有IO的操作给我单独的做上标记

def f(url):
print('GET: %s' % url)
resp = urllib.request.urlopen(url)
data = resp.read()
print('%d bytes received from %s.' % (len(data), url))

time_start = time.time()
url_i =['https://www.python.org/',
'https://github.com/',
'https://www.jenkins.io/']
for url in url_i:
f(url)
print('同步耗时:',time.time()-time_start)

now_time = time.time()
gevent.joinall([
gevent.spawn(f,'https://www.python.org/'),
gevent.spawn(f,'https://github.com/'),
gevent.spawn(f,'https://www.jenkins.io/'),
])
print('异步耗时:',time.time()-now_time)
上一篇:python – 为什么gevent.socket会破坏multiprocessing.connection的auth


下一篇:协程