5

What is the best way to connect two daemons in Python?

I have daemon A and B. I'd like to receive data generated by B in A's module (maybe bidirectional). Both daemons support plugins, so I'd like to shut communication in plugins. What's the best and cross-platform way to do that?

I know few mechanisms from low-level solutions - shared memory (C/C++), linux pipe, sockets (TCP/UDP), etc. and few high-level - queue (JMS, Rabbit), RPC.

Both daemons should run on the same host, but obviously better approach is to abstract from connection type.

What are typical solutions/libraries in python? I'm looking for an elegant and lightweight solution. I don't need external server, just two processes talking with each other.

What should I use in python to do that?

Simon
  • 2,329
  • 4
  • 30
  • 49
  • 1
    A message queue is most likely the most elegant solution. – ThiefMaster Jun 23 '12 at 21:14
  • Hmm as message queue I understand RabbitMQ/JMS, etc. I'd like to avoid external, third daemon/server to handle whole communication. Besides communication will be really intense, so maybe even if queue is the most elegant way, I think something simpler could be better. – Simon Jun 23 '12 at 21:22
  • 1
    I would say dbus, but there is now windows version of dbus. – dav1d Jun 23 '12 at 21:30
  • Unfortunately Windows is also my target. – Simon Jun 23 '12 at 21:40
  • could `subproccess.Popen()` work when used with `communicate()` for your purposes? something along the lines of spawn both programs with args and such from an overall program containing the subprocesses and communicate with eachother through the overall program? – IT Ninja Jun 23 '12 at 21:45
  • Not really. I don't want do any changes in those daemons, even I don't want to change a way how to start/stop them. Use case is trivial. I (administrator, cron job, whatever, something what is already configured) just run one of them with my plugin enabled, second one also. If plugin is able to connect to second one it sends data if not it produces warning in log or something like that. Right now I think any RPC/Socket solution would be the best. Queue is the most elegant, but not lightweight. Any recommendations for RPC/Socket(used on localhost) library in python? – Simon Jun 23 '12 at 21:53

2 Answers2

2

You can use sockets for process communication: http://docs.python.org/howto/sockets.html

Also remote procedure calls suits for that: Python XML RPC http://docs.python.org/library/xmlrpclib.html or Google Protobuf http://code.google.com/p/protobuf/ https://developers.google.com/protocol-buffers/docs/pythontutorial

Dmitry Trofimov
  • 7,371
  • 1
  • 30
  • 34
0

I am not sure how heavy is your traffic, but I would recommend the asyncore package. Its fairly simple to use and it is based on sockets.

I did a command-pattern with this long time ago. I can dig out the code if you are interested.

Juha
  • 2,053
  • 23
  • 44