Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python interprocess querying/control

Tags:

python

ipc

I have this Python based service daemon which is doing a lot of multiplexed IO (select).

From another script (also Python) I want to query this service daemon about status/information and/or control the processing (e.g. pause it, shut it down, change some parameters, etc).

What is the best way to send control messages ("from now on you process like this!") and query processed data ("what was the result of that?") using python?

I read somewhere that named pipes might work, but don't know that much about named pipes, especially in python - and whether there are any better alternatives.

Both the background service daemon AND the frontend will be programmed by me, so all options are open :)

I am using Linux.

like image 987
agnsaft Avatar asked Sep 27 '10 17:09

agnsaft


People also ask

How to communicate between two processes Python?

Passing Messages to Processes A simple way to communicate between process with multiprocessing is to use a Queue to pass messages back and forth. Any pickle-able object can pass through a Queue. This short example only passes a single message to a single worker, then the main process waits for the worker to finish.

What is IPC in Python?

Interprocess Communication and Networking — Python 3.6.

How do you implement inter process communication in Python?

Python library – PyCSP In the above PyCSP process network, there are two processes – Process1 and Process 2. These processes communicate by passing messages through two channels – channel 1 and channel 2.

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

asyncio — Asynchronous I/O.


1 Answers

Pipes and Named pipes are good solution to communicate between different processes. Pipes work like shared memory buffer but has an interface that mimics a simple file on each of two ends. One process writes data on one end of the pipe, and another reads that data on the other end.

Named pipes are similar to above , except that this pipe is actually associated with a real file in your computer.

More details at

  • http://www.softpanorama.org/Scripting/pipes.shtml

In Python, named pipe files are created with the os.mkfifo call

x = os.mkfifo(filename)

In child and parent open this pipe as file

out = os.open(filename, os.O_WRONLY)
in = open(filename, 'r')

To write

os.write(out, 'xxxx')

To read

lines = in.readline( )

Edit: Adding links from SO

  • Create a temporary FIFO (named pipe) in Python?
  • https://stackoverflow.com/search?q=python+named+pipes

You may want to read more on "IPC and Python"

  • http://www.freenetpages.co.uk/hp/alan.gauld/tutipc.htm
like image 199
pyfunc Avatar answered Sep 28 '22 00:09

pyfunc