Python 使用gevent下载图片案例

import urllib.request
import gevent
from gevent import monkey
monkey.patch_all()


def downloader(img_name, img_url):
    req = urllib.request.urlopen(img_url)
    img_content = req.read()
    with open(img_name, "wb") as f:
        f.write(img_content)


def main():
    gevent.joinall([
        gevent.spawn(downloader, "1.jpg", "http://i1.zhiaigou.com/uploads/tu/201908/9999/0f525ffb54.jpg"),
        gevent.spawn(downloader, "2.jpg", "http://i1.zhiaigou.com/uploads/tu/201908/9999/b4b40eb300.jpg")
    ])


if __name__ == '__main__':
    main()

 

posted @ 2019-08-13 22:21  李道臣  阅读(477)  评论(0编辑  收藏  举报