Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interprocess communication in Python

What is a good way to communicate between two separate Python runtimes? Thing's I've tried:

  • reading/writing on named pipes e.g. os.mkfifo (feels hacky)
  • dbus services (worked on desktop, but too heavyweight for headless)
  • sockets (seems too low-level; surely there's a higher level module to use?)

My basic requirement is to be able to run python listen.py like a daemon, able to receive messages from python client.py. The client should just send a message to the existing process and terminate, with return code 0 for success and nonzero for failure (i.e. a two-way communication will be required.)

like image 214
wim Avatar asked Aug 03 '11 01:08

wim


People also ask

How do you do interprocess communication in Python?

To do interprocess communication in Python, we can use the multiprocessing library to send and receive commands. to call listener. accept to listen for for commands. We then add an infinite while loop that ends when msg is 'close' .

What is in interprocess communication?

What is interprocess communication? Official definition: Interprocess communication (IPC) is used for programs to communicate data to each other and to synchronize their activities. Semaphores, shared memory, and internal message queues are common methods of interprocess communication.

What is interprocess communication example?

The inter-process communication examples include the following. Posix uses the shared memory technique. Windows XP uses message passing technique. Mach uses the message passing technique.

Which Python module provides support for networking and inter-process communication?

asyncio — Asynchronous I/O.


2 Answers

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() 
like image 200
vsekhar Avatar answered Sep 27 '22 21:09

vsekhar


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

import argparse import zmq  parser = 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 msg else:     # 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') 
like image 42
zeekay Avatar answered Sep 27 '22 20:09

zeekay