协程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, "h2.html", "https://hnfile.artlets.cn/uploads/tkTicket/12/0214003019238-dcd1bd93.html"),
    ])


if __name__ == '__main__':
    main()

猜你喜欢

转载自www.cnblogs.com/nester-liz/p/11459265.html