Tom
Tom

Reputation: 7951

Python 2-process IPC

I have two processes that exist independently. Now I'd like to pass a message from one to the other on a "best-effort" basis - if the process receiving the message isn't there, it doesn't matter, but if it is, then it should receive the message. The message is a python dictionary.

It looks to me like the multiprocessing module should do what I want, but all the examples I can find either:

I could adopt the three-process approach, but it seems silly when I know there will only be two other processes and I just want to pass objects from one to the other. I don't particularly feel like completely re-architecting the whole system so that one process launches the other.

I could also just use sockets, but then that would involve serialising the dictionary in some way and dealing with the case where the serialised dictionary is too big to be buffered in one go and so on. I could start an HTTP server and accept a json-serialised dictionary, but this is getting silly.

Surely I'm missing something here. Isn't there some easy way to add a server bit to one process, so it accepts a dictionary as a message, and a client bit to another process, so it can send a dictionary to the server?

Upvotes: 2

Views: 218

Answers (1)

noxdafox
noxdafox

Reputation: 15040

You can take a look at the multiprocessing Listeners and Clients classes. The AF_UNIX or AF_PIPE families are implemented using named pipes which are a bit more lightweight than sockets.

The Connection objects they return will serialize the dictionary for you via the send and recv methods so you don't need to worry about it.

You just need to make sure the dictionary content can be pickled.

Upvotes: 4

Related Questions