0

I want to interact with multiprocessing Queue data in multiple consoles like following:

#test.py
import multiprocessing
global queue
queue = multiprocessing.Queue()

#PYTHON CONSOLE 1
from test import queue
queue.put("This is console 1")

#PYTHON CONSOLE 2
from test import queue
print queue.get() #"This is console 1"

But this doesn't work.What am I missing?

1 Answer 1

1

If I understood correctly what you are trying to do, this cannot work. I'm assuming that by "console" you mean the Python interactive prompt.

  1. You start "console 1", which is an OS process. You create a Queue object in it, and put something on it.

  2. You start "console 2", which is an OS process that was not created through a Process object. You create a Queue object in it but this is a different object than the one created in "console 1". You try to get something from it but you get nothing because nothing has been put on it. (The fact that they both import test.py is irrelevant.)

Queue object is not meant to serve as a communication channel for processes that are not related by means of the Process object. See for instance, this example from the documentation:

from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print q.get()    # prints "[42, None, 'hello']"
    p.join()

Now how the 2nd process is created by using Process. The p process by being created through Process shares the same q queue with the original process.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!Then,is there any way to see the values of "q" after the p.start(). In my application,p.start() run the server so I can't use the same interactive console.
You could open a log file and write values to that file. That's typically how servers work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.