协程并发下载器

2020-04-21  本文已影响0人  木叶苍蓝
import gevent 
from gevent import monkey
import urllib.request

# 有耗时操作时需要
monkey.patch_all()

def my_download(url):
    print “GET: %s”%(url, )
    resp = urllib.request.urlopen(url)
    data = resp.read()
    print "%d bytes received from %s."%(len(data), url)

gevent.joinall([
    gevent.spawn(my_downlad, "http://www.baidu.con/"),
    gevent.spawn(my_downlad, "http://www.itcast.cn/"),
    gevent.spawn(my_downlad, "http://www.itheima.con/")
])
上一篇下一篇

猜你喜欢

热点阅读