python多线程依次下载思路,python多线程思路,我有一文件列表,我要开多


我有一文件列表,我要开多个线程,比如10个,依次下载列表中的文件,python怎么写?多谢!

python中的并发处理的话推荐用gevent代替多线程。

from gevent import monkeymonkey.patch_all()from gevent.pool import Pooldef download(url):    # download form urldef main():    # your files list    files = [...]    # 10 current with gevent Pool    pool = Pool(10)    pool.map(download, files)

download函数可以使用requests模块来实现下载功能。希望可以帮到你。

简单の来, 用concurrent模块

from concurrent.futures import *def download(url):    #bala bala    return urlwith ThreadPoolExecutor(max_workers=10) as executor:    res = executor.map(download, [1, 2, 3, 4])    for r in res:        #bala bala

编橙之家文章,

评论关闭