#网页爬取
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)