我具有无限多线程下载器,其工作方式如下:的Python:Multihreading下载阻断彼此
queue = Queue() # links for downloading
def downloader(queue):
while:
link = queue.get()
# do download with requests library
for i in range(4):
task = Thread(target=downloader, args=(queue,))
task.start()
队列被周期性地填充新数据。但是,当我打印出来哪个环节被下载,它看起来像线程不并行工作,例如:
time 0 - thread 1 - link 1
time 1 - thread 1 - link 2
time 2 - thread 1 - link 3
time 3 - thread 1 - link 4
time 4 - thread 2 - link 5
time 5 - thread 2 - link 6
time 6 - thread 2 - link 7
time 7 - thread 3 - link 8
time 8 - thread 3 - link 9
time 9 - thread 3 - link 10
time 10 - thread 1 - link 11
time 11 - thread 1 - link 12
我希望它会更喜欢:螺纹1 2 3 4 1 2 3 4服务器有4点。
感谢
你能显示请求代码吗? – Eloims
你如何衡量你的'时间'?用什么频率将链接插入'队列'? “下载程序”需要多长时间才能完成? (是否有可能每个'downloader'的执行完成_before_下一个链接插入'queue'?)另请注意,多线程并不一定使用所有的CPU。您还可以在单个CPU上进行多线程处理。 –
简而言之: 'session = requests.Session()' 'session.cookies = login_cookies.copy()' '链接的链接:' 'data = session.get(url,params = {... })' 'if data.status_code == 200:' 'fp = open(filepath,'wb')' 'fp.write(data.content)' 'fp.close()' – Greno