【发布时间】:2016-03-13 04:41:03
【问题描述】:
我在 python3 中为多个线程使用如下代码,我在 cpu_count() 中尝试了 2、3 和 4 次线程,但我不确定是否所有这些线程都在使用,我如何检查是否有一些队列从未使用过?
queue = Queue()
for x in range(cpu_count() * 2):
worker = DownloadWorker(queue)
worker.daemon = True
worker.start()
queue.join()
class DownloadWorker(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
link, download_path = self.queue.get()
download_link(link, download_path)
self.queue.task_done()
def downloadImage(imageServer, imageLocal, queue):
queue.put((imageServer, imageLocal))
【问题讨论】:
-
在上面的代码示例中,您只有一个传递给所有工作人员的队列。DownloadWorker 类是什么样的?
-
我修改了问题。顺便问一下,如果thread1、3、4都在等待,是不是应该接下来执行thread1?
-
什么是
self.queue.task_done,队列中的元素是如何填充的? -
queue.task_done 是队列而不是我的。
标签: multithreading python-3.x queue