7Python全栈之路系列之协程
from urllib import requestfrom gevent import monkey
import gevent
import time
monkey.patch_all()# 当前程序中只要设置到IO操作的都做上标记
def wget(url):
print('GET: %s' % url)
resp = request.urlopen(url)
data = resp.read()
print('%d bytes received from %s.' % (len(data), url))
urls = [
'https://www.python.org/',
'https://www.python.org/',
'https://github.com/',
'https://blog.ansheng.me/',
]
# 串行抓取
start_time = time.time()
for n in urls:
wget(n)
print("串行抓取使用时间:", time.time() - start_time)
# 并行抓取
ctrip_time = time.time()
gevent.joinall([
gevent.spawn(wget, 'https://www.python.org/'),
gevent.spawn(wget, 'https://www.python.org/'),
gevent.spawn(wget, 'https://github.com/'),
gevent.spawn(wget, 'https://blog.ansheng.me/'),
])
print("并行抓取使用时间:", time.time() - ctrip_time)
页:
[1]