Interprocess communication in Python Interprocess communication in Python python 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 Listeneraddress = ('localhost', 6000)     # family is deduced to be 'AF_INET'listener = Listener(address, authkey='secret password')conn = listener.accept()print 'connection accepted from', listener.last_acceptedwhile True:    msg = conn.recv()    # do something with msg    if msg == 'close':        conn.close()        breaklistener.close()

Your client could send commands as objects:

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


Nah, zeromq is the way to go. Delicious, isn't it?

import argparseimport zmqparser = argparse.ArgumentParser(description='zeromq server/client')parser.add_argument('--bar')args = parser.parse_args()if args.bar:    # client    context = zmq.Context()    socket = context.socket(zmq.REQ)    socket.connect('tcp://127.0.0.1:5555')    socket.send(args.bar)    msg = socket.recv()    print msgelse:    # server    context = zmq.Context()    socket = context.socket(zmq.REP)    socket.bind('tcp://127.0.0.1:5555')    while True:        msg = socket.recv()        if msg == 'zeromq':            socket.send('ah ha!')        else:            socket.send('...nah')


From my experience, rpyc is by far the simplest and most elegant way to go about it.

(I know this is an old question, but I've just stumbled upon it..)