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 13 1815
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
skipOr make the consumers daemon threads so that when the producers
skipare finished an all non-daemon threads exit, the consumers do as
skipwell.
Forget that I wrote this. If they happen to be working on the token they've
consumed at the time the other threads exit, they will as well. Use the
sentinel token idea instead.
Skip
On Aug 27, 1:06*pm, Gerhard Häring <g...@ghaering.dewrote:
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
Your solution works assuming that you know how many consumer threads
you have :). I don't :). More than that, it's not correct if you have
more than one producer :). Having a sentinel was my very first idea,
but as you see... it's a race condition (there are cases in which not
all items are processed).
Your solution works assuming that you know how many consumer threads
you have :). I don't :). More than that, it's not correct if you have
more than one producer :). Having a sentinel was my very first idea,
but as you see... it's a race condition (there are cases in which not
all items are processed).
Queue raises an Empty exception when there are no items left in the
queue. Put the q.get() call in a try block and exit in the except
block.
You can also use a condition variable to signal threads to terminate.
On Aug 27, 2:54*pm, Jeff <jeffo...@gmail.comwrote:
Queue raises an Empty exception when there are no items left in the
queue. *Put the q.get() call in a try block and exit in the except
block.
Wrong. What if producer takes a long time to produce an item?
Consumers
will find the queue empty and exit instead of waiting.
You can also use a condition variable to signal threads to terminate.
This is the solution I want to avoid because it duplicates Queue's
functionality.
I prefer having a clean solution with nice design to hacking Queue
class.
On Aug 27, 12:45*pm, Alexandru Mosoi <brtz...@gmail.comwrote:
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
ok. I think I figured it out :). let me know what you think
global num_tasks, num_done, queue
num_tasks = 0
num_done = 0
queue = Queue()
# producer
num_tasks += 1
for i in items:
num_tasks += 1
queue.put(i)
num_tasks -= 1
if num_tasks == num_done:
queue.put(None)
# consumer
while True:
i = queue.get()
if i is None:
queue.put(None)
break
# do stuff
num_done += 1
if num_done == num_tasks:
queue.put(None)
break
>
Your solution works assuming that you know how many consumer threads
you have :). I don't :). More than that, it's not correct if you have
more than one producer :). Having a sentinel was my very first idea,
but as you see... it's a race condition (there are cases in which not
all items are processed).
If you have several producers, how do you coordinate when to shut down?
Apart from that, you can easily solve the problem of not knowing how many
consumers you have by making a consumer stuff back the sentinel into the
queue. Then it will ripple down until no consumer is left.
Diez
On Aug 27, 1:17 pm, Alexandru Mosoi <brtz...@gmail.comwrote:
On Aug 27, 12:45 pm, Alexandru Mosoi <brtz...@gmail.comwrote:
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
ok. I think I figured it out :). let me know what you think
global num_tasks, num_done, queue
num_tasks = 0
num_done = 0
queue = Queue()
# producer
num_tasks += 1
for i in items:
num_tasks += 1
queue.put(i)
num_tasks -= 1
if num_tasks == num_done:
queue.put(None)
# consumer
while True:
i = queue.get()
if i is None:
queue.put(None)
break
# do stuff
num_done += 1
if num_done == num_tasks:
queue.put(None)
break
Are you sure you want to put the final exit code in the consumer?
Shouldn't the producer place a None on the queue when it knows it's
finished? The way you have it, the producer could make 1 item, it
could get consumed, and the consumer exit before the producer makes
item 2.
Iain
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
ok. I think I figured it out :). let me know what you think
global num_tasks, num_done, queue
num_tasks = 0
num_done = 0
queue = Queue()
# producer
num_tasks += 1
for i in items:
num_tasks += 1
queue.put(i)
what's the point of using a thread-safe queue if you're going to use a
non-thread-safe counter? if you want to write broken code, you can do
that in a lot fewer lines ;-)
as others have mentioned, you can use sentinels: http://effbot.org/librarybook/queue.htm
or, in Python 2.5 and later, the task_done/join pattern shown here: http://docs.python.org/lib/QueueObjects.html
</F> sk**@pobox.com writes:
Or make the consumers daemon threads so that when the producers are finished
an all non-daemon threads exit, the consumers do as well.
How are the consumers supposed to know when the producers are
finished? Yes, there are different approaches like sentinels, but the
absence of a unified approach built into the library really does seem
like a deficiency in the library.
On Aug 27, 4:55*pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
s...@pobox.com writes:
Or make the consumers daemon threads so that when the producers are finished
an all non-daemon threads exit, the consumers do as well.
How are the consumers supposed to know when the producers are
finished? *Yes, there are different approaches like sentinels, but the
absence of a unified approach built into the library really does seem
like a deficiency in the library.
See effbot's reply. The task_done and join methods were put there for
exactly this use case.
Raymond This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: phil |
last post by:
And sorry I got ticked, frustrating week
>And I could help more, being fairly experienced with
>threading issues and race conditions and such, but
>as I tried to indicate in the first place,...
|
by: Brian Henry |
last post by:
If i inherite a queue class into my class, and do an override of the enqueue
member, how would i then go about actually doing an enqueue of an item? I am
a little confused on this one... does over...
|
by: alisaee |
last post by:
plz check what i have made wrong what is requierd her is to creat class
queue and class stack and run the push,pop operation .
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class...
|
by: Kceiw |
last post by:
Dear all,
When I use #include "queue.h", I can't link it.
The error message follows:
Linking...
G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In
function `main':...
|
by: Rooro |
last post by:
Hello everyone
i'm working on :
"
Familiar childhood games such as hide and Go Seek and Tag involve determining the player who is to be "It". One method has the players stand in a circle...
|
by: lavender |
last post by:
When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10.
Where is the wrong in my code? Why it cannot...
|
by: jrpfinch |
last post by:
I have a script which is based on the following code. Unfortunately,
it only works on Python 2.3 and not 2.5 because there is no esema or
fsema attribute in the 2.5 Queue. I am hunting through...
|
by: j_depp_99 |
last post by:
Thanks to those guys who helped me out yesterday. I have one more
problem; my print function for the queue program doesnt work and goes
into an endless loop. Also I am unable to calculate the...
|
by: ecestd |
last post by:
how do you implement a copy constructor for this pointer-based ADT
queue
#include <cassert // for assert
#include <new // for bad_alloc
using namespace std;
//private:{Queue::Queue(const...
|
by: ecestd |
last post by:
I did implement the copy constructor but still have a problem with
it. It is not working. What could be wrong?
#include "QueueP.h"
#include <cassert // for assert
#include <new // for...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |