3

I have a process that creates several subprocesses with which it needs to coummunicate in Python 2.5. I use the subprocess module to start the processes, setting stdin and stdout to subprocess.PIPE. So far so good.

In a loop, I then, run select.select() on each subproccess's stdout stream waiting for them to tell me they're ready. I then write to their stdin to give them work and repeat the process.

At the moment, I am simply using stdin.write and stdout.readline to preform the communication. However, I'd like to be able to communicate complex messages between the processes. Simple newline terminated messages won't suffice (unless I take strides to escape new lines in the messages somehow). I am thinking I can prefix all messages with the length in bytes so that my messages look something like:

6:foobar

But this brings me to my question: does something like this already exist? I don't really want to reinvent the wheel here. I want a library that tells me when there's a complete message ready and hands it over to me. There's plenty of other protocols that do things like this in various ways (TCP, Message Queue Servers, HTTP, etc) but they're all overkill for my use case. What's the right way to do message passing between processes in Python?

2 Answers 2

2

You could use json or pickle to encode the data structure that contains the message, and then use the same on the other side to read the message.

Sign up to request clarification or add additional context in comments.

4 Comments

Sure, but I still need to worry about escaping or in some other way defining the boundaries of the message, no? (Maybe JSON escapes the new lines, I'm not sure about Pickle.) I also want to make sure that, if the message comes through in pieces, due to buffering, I don't miss anything.
If the messages aren't too large and don't come too quickly, I don't see a problem with letting select handle it. If you find a problem with the message rate then you may want to look at a progressive parser which may be able to handle concatenated messages.
A quick look at cPickle shows me that it puts new lines in its strings which means its off the table by itself: >>> print pickle.dumps("a\nb") S'a\nb' p1 .
You could encode it with the string-escape codec before outputting it.
1

There are frameworks that really help you to address this kind of functionalities. If you want to send more complex messages without bother about sync issues. Some examples of really good frameworks are:

These frameworks, mostly, work on top of TCP/IP. Slightly different approach to your stuff. But, maybe something you should consider.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.