标签:com tar urllib data 操作 nal imp receive event
#! /usr/bin/env python3 # -*- coding:utf-8 -*- from urllib import request import gevent,time from gevent import monkey monkey.patch_all() # 把当前程序中的所有io操作都做上标记 def spider(url): print("GET:%s" % url) resp = request.urlopen(url) data = resp.read() print("%s bytes received from %s.." % (len(data), url)) urls = [ "https://weibo.com/", "https://www.baidu.com/", "https://www.yinxiang.com/" ] start_time = time.time() for url in urls: spider(url) print("同步耗时:",time.time() - start_time) async_time_start = time.time() gevent.joinall([ gevent.spawn(spider,"https://weibo.com/"), gevent.spawn(spider,"https://www.baidu.com/"), gevent.spawn(spider,"https://www.yinxiang.com/"), ]) print("异步耗时:",time.time() - async_time_start)
输出
GET:https://weibo.com/ 6117 bytes received from https://weibo.com/.. GET:https://www.baidu.com/ 227 bytes received from https://www.baidu.com/.. GET:https://www.yinxiang.com/ 16505 bytes received from https://www.yinxiang.com/.. 同步耗时: 0.23460006713867188 GET:https://weibo.com/ GET:https://www.baidu.com/ GET:https://www.yinxiang.com/ 227 bytes received from https://www.baidu.com/.. 16505 bytes received from https://www.yinxiang.com/.. 6117 bytes received from https://weibo.com/.. 异步耗时: 0.0946660041809082
标签:com tar urllib data 操作 nal imp receive event
原文地址:https://www.cnblogs.com/sea-stream/p/10317051.html