tags:

views:

50

answers:

1

Hi,

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.

+1  A: 

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

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

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

pyfunc
Thanks mate. Is this the preferred way for doing the task I described above?
invictus
@invictus: You could send a lot of custom messages this way which the process can handle. But for shutting down etc it is better to use signals with signal handlers in the process.
pyfunc
If I need multiple frontends communicating with the service at the same time, I would need multiple named pipes as well in order to "address" the query results correctly?
invictus
Also, does not pipes support two-way read/write?
invictus
@invictus : Yes it does support two-way read and write. The setting depends on what you want to achieve with it. If you had multiple frontend communicating with the service, I would suggest a socket based solution, where a server could listen, connect and respond to command messages
pyfunc
Like Unix Domain Sockets? I am guessing not network sockets
invictus