Queue() question. This post is pretty long.

G

grocery_stocker

I don't get how item = self.__queue.get() gets advanced to

if item is None:

in the following code.
.... def __init__(self, queue):
.... Thread.__init__(self)
.... self.__queue = queue
.... def run(self):
.... while 1:
.... item = self.__queue.get()
.... if item is None:
.... break
.... print "task", item, "finished"
....
.... Worker(queue).start()
........ queue.put(i)
....task 1 finished
task 2 finished
task 3 finished
task 4 finished
task 5 finished
task 6 finished
task 7 finished
task 8 finished
task 9 finished.... queue.put(None)
....

This because when I do something like

[cdalten@localhost ~]$ python
Python 2.4.3 (#1, Oct 1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... item = queue.get()
.... if item == 'done':
.... break
....
this
is
a
test
done

[3]+ Stopped python
[cdalten@localhost ~]$ kill %3

It appears that only item = queue.get() called. Ie, the block never
advances to if item == 'done':
like in the first example
 
P

Piet van Oostrum

grocery_stocker said:
g> [cdalten@localhost ~]$ python
g> Python 2.4.3 (#1, Oct 1 2006, 18:00:19)
g> [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
g> Type "help", "copyright", "credits" or "license" for more information.g> ... item = queue.get()
g> ... if item == 'done':
g> ... break
g> ...
g> this
g> is
g> a
g> test
g> done
g> [3]+ Stopped python
g> [cdalten@localhost ~]$ kill %3
g> It appears that only item = queue.get() called. Ie, the block never
g> advances to if item == 'done':
g> like in the first example

You don't put anything in the queue here. The lines that you type there
don't get magically into the queue. So therefore the queue.get blocks.
 
G

grocery_stocker

grocery_stocker <[email protected]> (g) wrote:
g> [cdalten@localhost ~]$ python
g> Python 2.4.3 (#1, Oct 1 2006, 18:00:19)
g> [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
g> Type "help", "copyright", "credits" or "license" for more information.
import Queue
queue = Queue.Queue()
while 1:
g> ... item = queue.get()
g> ... if item == 'done':
g> ... break
g> ...
g> this
g> is
g> a
g> test
g> done
g> [3]+ Stopped python
g> [cdalten@localhost ~]$ kill %3
g> It appears that only item = queue.get() called. Ie, the block never
g> advances to if item == 'done':
g> like in the first example

You don't put anything in the queue here. The lines that you type there
don't get magically into the queue. So therefore the queue.get blocks.
--

Yeah my bad. I misread the first code -(.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top