我有一个无限的多线程下载程序,它的工作方式如下:
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,12,3,4,服务器有4个cpus。
谢谢
发布于 2017-01-10 09:18:40
重复检查输出文件和每个线程正在写入的位置。写锁可能导致代码以比预期更多的顺序执行。
https://stackoverflow.com/questions/41564768
复制相似问题