Reputation: 40892
I have a subprocess function (called parseContents) that gets called using the following code:
def main():
p = Process(target=parseContents, args=(event.name,))
p.start()
p.join()
Utilizing the multiprocessing module and the Queue method, how would I go about passing a variable from parseContents back to main to be used after the p.join() line?
I've read that I would use:
from multiprocessing import Queue
queue = Queue()
queue.put( myVar ) #obviously this would be inside parseContents()
print queue.get( myVar ) #obviously this would be inside main()
Do I need to pass the 'queue' variable/instance to my parseContents function after calling it in my main so the child process knows about the queue?
What would be a correct implementation of a multiprocessing queue between parent and child processes as constructed in the main():
segment above?
Upvotes: 3
Views: 2577
Reputation: 20232
To use a Queue in this way you have to instantiate it in main()
and pass it to parseContents
as an argument. Once you do that you should be able to use the code you have (in the correct places) to pass the item from the child process to the parent process.
The Python docs warn that you should not attempt to join the child process if the Queue is not empty, so make sure to get all of the items out of the queue before you call join
. In fact, you should be able to run the code without calling join
at all because queue.get
will wait until there is an item in the Queue.
Upvotes: 1