python 多协程异步IO爬取网页加速3倍。

 1 from urllib import request
 2 import gevent,time
 3 from gevent import monkey#该模块让当前程序所有io操作单独标记,进行异步操作。
 4 
 5 monkey.patch_all()#对当前程序的io操作打上补丁。没有该monkey方法,异步IO无效。
 6 def  f(url):
 7     print('GET:%s'%url)
 8     resp = request.urlopen(url)#获取网页
 9     data = resp.read()#读取网页
10     print('%d bytes received from  %s'%(len(data),url))#打印长度
11 url = ['https://www.yahoo.com/','https://www.python.org/',
12  'https://github.com/']
13 start = time.time()
14 for i in url:
15     f(i)#循环运行列表中的网页
16 print('串行执行时间:',time.time() - start)#串行执行时间
17 async_time = time.time()
18 gevent.joinall([
19     gevent.spawn(f,'https://www.yahoo.com/')#异步执行启动协程
20 ,   gevent.spawn(f,'https://www.python.org/'),
21     gevent.spawn(f,'https://github.com/'),
22 ])
23 print('异步执行时间async time:',time.time() - async_time)#多协程异步IO执行时间


 

以下为运行结果,明显多协程的牛逼之处。。。。。。。如果不执行monkey方法,则异步IO就会按串行执行。
C:\Users\hushuning\Anaconda3\python.exe C:/Users/hushuning/PycharmProjects/untitled/njx/把当前程序的所有的io操作单独标记,进行异步操作.py
GET:https://www.yahoo.com/
510125 bytes received from  https://www.yahoo.com/
GET:https://www.python.org/
48857 bytes received from  https://www.python.org/
GET:https://github.com/
51373 bytes received from  https://github.com/
串行执行时间: 4.710935354232788
GET:https://www.yahoo.com/
GET:https://www.python.org/
GET:https://github.com/
48857 bytes received from  https://www.python.org/
512422 bytes received from  https://www.yahoo.com/
51373 bytes received from  https://github.com/
异步执行时间async time: 1.6521050930023193

Process finished with exit code 0
 
  

 

 

 

转载于:https://www.cnblogs.com/hushuning/p/7922220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值