Skip to content Skip to sidebar Skip to footer

Python: Multiprocessing Queue Seemingly Unaccessible

I am confused why this code seems to hang and do nothing? As I try to experiment, it seems that I cannot get functions to access the queue from outside the function that added the

Solution 1:

Hmmm. I tested the example I gave you on Linux, and your second block code, which you say doesn't work, does work for me on Linux. I looked up the docs and indeed, Windows appears to be a special case:

Global variables

Bear in mind that if code run in a child process tries to access a global variable, then the value it sees (if any) may not be the same as the value in the parent process at the time that Process.start was called.

However, global variables which are just module level constants cause no problems.

Solution

I can't test this because I don't have your operating system available, but I would try passing the queue then to each of your functions. To follow your example:

from multiprocessing import Queue, Process

defmain():
    q = Queue()

    putter_process = Process(target=putter, args=(q,))
    putter_process.start()
    print(q.get(block=True) + ' gotten')
    print(q.get(block=True) + ' gotten')

defputter(q):
    q.put('a')
    q.put('b')

if __name__ == '__main__':
    main()

Based on my interpretation of the docs, that should work on Windows, but I can't say I've tested it myself.

Note on tuples

In your other question, you asked why the trailing comma is relevant in args=. Let me show you:

>>> my_string = 'hello, world!'>>> type(my_string)
<class'str'>
>>> print(my_string)
hello, world!

As you can see, I've created a string variable. Now, let me create a tuple, which is like a list, but varies in some important ways.

>>> my_tuple = ('hello, world', 'hola mundo')
>>> type(my_tuple)
<class'tuple'>
>>> print(my_tuple)
('hello, world', 'hola mundo')

As you can see, I've created a tuple with two elements, each element being a string. But what if I want to create a tuple with just one element? This will not work:

>>> my_not_a_tuple = ('hello, world')
>>> type(my_not_a_tuple)
<class'str'>
>>> print(my_not_a_tuple)
hello, world

As you can see, I've only created a string. But I can create a tuple with one element by adding a comma, which clarifies that the parentheses are there to show that it's a tuple, not to control the order of operations:

>>> my_tuple = ('hello, world',)
>>> type(my_tuple)
<class'tuple'>
>>> print(my_tuple)
('hello, world',)

That extra comma matters because you're needing to pass a tuple, not a string.

Post a Comment for "Python: Multiprocessing Queue Seemingly Unaccessible"