队列的使用[英] use of Queue

本文是小编为大家收集整理的关于队列的使用的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

Queue 打算如何使用?我在python中找到了以下代码
手册,但我不明白如何在所有项目之后阻止消费者
已经产生.我尝试了不同的方法,但都是
似乎不正确(竞争、死锁或重复队列功能)
def worker():
而真:
项目 = q.get()
做工作(项目)
q.task_done()

q = 队列()
对于我在范围内(num_worker_threads):
t = 线程(target=worker)
t.setDaemon(True)
t.start()

对于 source() 中的项目:
q.put(项目)

q.join() # 阻塞直到所有任务完成

推荐答案

Alexandru Mosoi 写道:
Queue 打算如何使用?我在python中找到了以下代码
手册,但我不明白如何在所有项目之后阻止消费者
已经产生.我尝试了不同的方法,但都是
似乎不正确(竞争、死锁或重复队列功能)
def worker():
而真:
项目 = q.get()
做工作(项目)
q.task_done()

q = 队列()
对于我在范围内(num_worker_threads):
t = 线程(target=worker)
t.setDaemon(True)
t.start()

对于 source() 中的项目:
q.put(项目)

q.join() # 阻塞直到所有任务完成
将哨兵放入被解释为"终止"的队列中.对于
工人.你当然需要为每个工人放一次.

Diez

Alexandru Mosoi 写道:
Queue 打算如何使用?我在python中找到了以下代码
手册,但我不明白如何在所有项目之后阻止消费者
已经产生.我尝试了不同的方法,但都是
似乎不正确(竞争、死锁或重复队列功能)
def worker():
而真:
项目 = q.get()
如果项目为无:
打破
做工作(项目)
q.task_done()

q = 队列()
对于我在范围内(num_worker_threads):
t = 线程(target=worker)
t.setDaemon(True)
t.start()

对于 source() 中的项目:
q.put(项目)
# 停止所有消费者
对于我在范围内(num_worker_threads):
q.put(None)
>
q.join() # 阻塞直到所有任务完成
我就是这样做的.

-- 格哈德



DiezPut 一个哨兵进入被解释为"终止"的队列
为工人而死.你当然需要把它放在那里一次
Diezeach 工人.

或者让消费者守护线程,以便当生产者完成时
所有非守护线程都退出,消费者也会退出.

跳过

本文地址:https://www.itbaoku.cn/post/1050785.html

问题描述

how is Queue intended to be used? I found the following code in python
manual, but I don''t understand how to stop consumers after all items
have been produced. I tried different approaches but all of them
seemed incorrect (race, deadlock or duplicating queue functionality)
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()

q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.setDaemon(True)
t.start()

for item in source():
q.put(item)

q.join() # block until all tasks are done

推荐答案

Alexandru Mosoi wrote:
how is Queue intended to be used? I found the following code in python
manual, but I don''t understand how to stop consumers after all items
have been produced. I tried different approaches but all of them
seemed incorrect (race, deadlock or duplicating queue functionality)
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()

q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.setDaemon(True)
t.start()

for item in source():
q.put(item)

q.join() # block until all tasks are done
Put a sentinel into the queue that gets interpreted as "terminate" for the
workers. You need of course to put it in there once for each worker.

Diez

Alexandru Mosoi wrote:
how is Queue intended to be used? I found the following code in python
manual, but I don''t understand how to stop consumers after all items
have been produced. I tried different approaches but all of them
seemed incorrect (race, deadlock or duplicating queue functionality)
def worker():
while True:
item = q.get()
if item is None:
break
do_work(item)
q.task_done()

q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.setDaemon(True)
t.start()

for item in source():
q.put(item)
# stop all consumers
for i in range(num_worker_threads):
q.put(None)
>
q.join() # block until all tasks are done
This is how I do it.

-- Gerhard



DiezPut a sentinel into the queue that gets interpreted as "terminate"
Diezfor the workers. You need of course to put it in there once for
Diezeach worker.

Or make the consumers daemon threads so that when the producers are finished
an all non-daemon threads exit, the consumers do as well.

Skip