2

I have a code snippet taken from another stackoverflow post Python Workers and Queues

from multiprocessing import Process
from Queue import Queue

class Worker(Process):
    def __init__(self, queue):
        super(Worker, self).__init__()
        self.queue= queue

    def run(self):
        print 'Worker started'
        # do some initialization here

        print 'Computing things!'
        for data in iter( self.queue.get, None ):
            print(data)

if __name__ == '__main__':       
    request_queue = Queue()
    for i in range(4):
        Worker( request_queue ).start()
    for data in range(100):
        request_queue.put( data )
    # Sentinel objects to allow clean shutdown: 1 per worker.
    for i in range(4):
        request_queue.put( None ) 

Why does this process hang and not process the queue contents?

Community
  • 1
  • 1
Corey Hahn
  • 81
  • 5
  • Works for me!!, What's the error? – Kashif Siddiqui Mar 16 '17 at 04:18
  • shouldn't it print out the numbers 0-99? I do not get the numbers to print out it is hung at the line --- for data in iter( self.queue.get, None ) Like it iterated on the Queue but since the new data arrives after the iteration it does not see the new data. – Corey Hahn Mar 16 '17 at 05:09

1 Answers1

0

Found my error. I did not know there are two Queues.

changing

 from multiprocessing import Process
 from Queue import Queue

to

 from multiprocessing import Process,Queue

Now I have the expected behavior

Corey Hahn
  • 81
  • 5