python多线程问题

2024-04-25 23:04:33 发布

您现在位置:Python中文网/ 问答频道 /正文

有谁能告诉我为什么这段代码在启动线程后生成队列?基本上,队列是在for循环之后生成的,但它已经在ThreadUrl类中使用了队列.get()方法。这是怎么回事?它如何从尚未生成的队列中获取值?在

for i in range(5):
    t = ThreadUrl(queue, out_queue)
    t.setDaemon(True)
    t.start()

# This is what confuses me! Shouldn't it be above the for loop??
for host in hosts:
    queue.put(host)

for i in range(5):
    dt = DatamineThread(out_queue)
    dt.setDaemon(True)
    dt.start()

#wait on the queue until everything has been processed
queue.join()
out_queue.join()

这是完整的来源

^{pr2}$

Tags: the代码intruehostfor队列queue
1条回答
网友
1楼 · 发布于 2024-04-25 23:04:33

host = self.queue.get()阻止执行线程,直到某个元素出现在queue中。在

所以

#spawn a pool of threads, and pass them queue instance
for i in range(5):
    t = ThreadUrl(queue, out_queue)
    t.setDaemon(True)
    t.start()

创建5个等待队列中任何元素的线程。在

^{pr2}$

填充队列。在此之后,线程开始处理。在

相关问题 更多 >

    热门问题