Can we use standard multiprocessing.Queue class for communicating between independent Python3 scripts like
bash% ./aaa.py &
bash% ./bbb.py &
If no, Is there another library like standard multiprocessing module for communicating with independent scripts.
-
I chose multiprocessing.managers module, its sample code url is docs.python.org/3.5/library/…shiina– shiina2016年04月06日 04:41:07 +00:00Commented Apr 6, 2016 at 4:41
2 Answers 2
Take a look at the section on "Listeners and Clients" in the multiprocessing docs; unlike the higher level simpler APIs, these APIs allow you to establish connections by address and authenticate the paired process, which allows two Python scripts to cooperate without having a parent/child relationship.
3 Comments
subprocess and/or multiprocessing's source code (the high level stuff is all in Python anyway) to get an idea of what they do and the proper way to solve your specific problem.It's a self answer. My minimum example is below.
aaa.py
from multiprocessing.managers import BaseManager
import queue
queue_a = queue.Queue()
queue_b = queue.Queue()
BaseManager.register('queue_a', callable=lambda: queue_a)
BaseManager.register('queue_b', callable=lambda: queue_b)
m = BaseManager(address=('', 50000), authkey=b'qwerty')
m.start()
shared_queue_a = m.queue_a()
shared_queue_b = m.queue_b()
shared_queue_a.put("How are you?")
msg = shared_queue_b.get()
print("%s: %s" % (__file__, msg))
m.shutdown()
bbb.py
from multiprocessing.managers import BaseManager
BaseManager.register('queue_a')
BaseManager.register('queue_b')
m = BaseManager(address=('localhost', 50000), authkey=b'qwerty')
m.connect()
queue_a = m.queue_a()
queue_b = m.queue_b()
msg = queue_a.get()
print("%s: %s" % (__file__, msg))
queue_b.put("I'm fine.")
Run these scripts.
./aaa.py &; sleep 1; ./bbb.py
Prints
./bbb.py: How are you?
./aaa.py: I'm fine.