博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
异步 携程 网络小爬虫
阅读量:5253 次
发布时间:2019-06-14

本文共 1262 字,大约阅读时间需要 4 分钟。

from urllib import request import gevent,time from gevent import monkey monkey.patch_all() #把当前程序的所有的io操作给我单独的做上标记 def f(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.yahoo.com/',         'https://github.com/' ] time_start = time.time() for url in urls:     f(url) print("同步cost",time.time() - time_start) async_time_start = time.time() gevent.joinall([     gevent.spawn(f, 'https://www.python.org/'),     gevent.spawn(f, 'https://www.yahoo.com/'),     gevent.spawn(f, 'https://github.com/'), ]) print("异步cost",time.time() - async_time_start)

GET: https://www.python.org/

50114 bytes received from https://www.python.org/.
GET: https://www.yahoo.com/
505000 bytes received from https://www.yahoo.com/.
GET: https://github.com/
65396 bytes received from https://github.com/.
同步cost 3.5022003650665283
GET: https://www.python.org/
GET: https://www.yahoo.com/
GET: https://github.com/
65396 bytes received from https://github.com/.
50114 bytes received from https://www.python.org/.
504996 bytes received from https://www.yahoo.com/.
异步cost 1.332076072692871

转载于:https://www.cnblogs.com/rongye/p/9983491.html

你可能感兴趣的文章
编程算法 - 左旋转字符串 代码(C)
查看>>
IOS解析XML
查看>>
Python3多线程爬取meizitu的图片
查看>>
树状数组及其他特别简单的扩展
查看>>
zookeeper适用场景:分布式锁实现
查看>>
110104_LC-Display(液晶显示屏)
查看>>
httpd_Vhosts文件的配置
查看>>
php学习笔记
查看>>
普通求素数和线性筛素数
查看>>
PHP截取中英文混合字符
查看>>
【洛谷P1816 忠诚】线段树
查看>>
电子眼抓拍大解密
查看>>
poj 1331 Multiply
查看>>
tomcat7的数据库连接池tomcatjdbc的25个优势
查看>>
Html 小插件5 百度搜索代码2
查看>>
P1107 最大整数
查看>>
多进程与多线程的区别
查看>>
Ubuntu(虚拟机)下安装Qt5.5.1
查看>>
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
java 常用命令
查看>>