Reputation: 8630
I am trying to send data to a multi processing Process, using a queue. For some reason, the example does not work. Do you know why?
I expect the program to print out "Received something: hello" and "Received poison pill", but it never gets there. However, it does print "running" and "listening", so I know that it definitely tries to read something from the queue.
I am using Pythong 3.4
from multiprocessing import Process
import queue
import time
class EventAggregatorProcess(Process):
def __init__(self):
super(EventAggregatorProcess, self).__init__()
self.event_queue = queue.Queue()
def run(self):
print('running')
is_stopped=False
while not is_stopped:
print('listening')
msg = self.event_queue.get()
if msg:
print('Received something: %s'%msg)
else:
print( 'Received poison pill' )
is_stopped=True
if __name__=='__main__':
eap = EventAggregatorProcess()
eap.start()
time.sleep(1)
eap.event_queue.put('hello')
eap.event_queue.put(None)
Upvotes: 2
Views: 41
Reputation: 5830
queue module is for multi-threaded programs. These threads would all be in a single process, meaning they also share memory. (see the See Also section at the end of the docs) Your program is multiprocessing
meaning you have multiple processes. These processes do not share memory. You should use the Queue from the multiprocessing
library. This version of Queue
handles the extra work needed for inter process communication.
from multiprocessing import Process, Queue
Upvotes: 2