Interprocess Communication in Python

Interprocess communication in Python

The multiprocessing library provides listeners and clients that wrap sockets and allow you to pass arbitrary python objects.

Your server could listen to receive python objects:

from multiprocessing.connection import Listener

address = ('localhost', 6000) # family is deduced to be 'AF_INET'
listener = Listener(address, authkey=b'secret password')
conn = listener.accept()
print 'connection accepted from', listener.last_accepted
while True:
msg = conn.recv()
# do something with msg
if msg == 'close':
conn.close()
break
listener.close()

Your client could send commands as objects:

from multiprocessing.connection import Client

address = ('localhost', 6000)
conn = Client(address, authkey=b'secret password')
conn.send('close')
# can also send arbitrary objects:
# conn.send(['a', 2.5, None, int, sum])
conn.close()

Interprocess communication between independent Python3 scripts

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.

Interprocess communication with a modified python interpreter

Going with the other answer of mine turned out to be a mistake. Because of how handles are inherited in python2 on Windows I couldn't get the same solution to work on Windows machines. I ended up using the far superior Listener and Client interfaces also found in the multiprocessing module.

This question of mine discusses that mistake.

simple inter-process communication

In general you are interested in sockets. A good place to get just the needed rough information is the documentation of IO::Socket::INET or more basic socket-stuff in perl from perldoc perlipc



Related Topics



Leave a reply



Submit