Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS-independent Inter-program communication between Python and C

I have very little idea what I'm doing here, I've never done anything like this before, but a friend and I are writing competing chess programs and they need to be able to communicate to each other.

He'll be writing mainly in C, the bulk of mine will be in Python, and I can see a few options:

  • Alternately write to a temp file, or successive temp files. As the communication won't be in any way bulky this could work, but seems like an ugly work-around to me, the programs will have to keep checking for change/new files, it just seems ugly.
  • Find some way of manipulating pipes i.e. mine.py| ./his . This seems like a bit of a dead end.
  • Use sockets. But I don't know what I'd be doing, so could someone give me a pointer to some reading material? I'm not sure if there are OS-independent, language independent methods. Would there have to be some kind of supervisor server program to administrate?
  • Use some kind of HTML protocol, which seems like overkill. I don't mind the programs having to run on the same machine.

What do people recommend, and where can I start reading?

like image 941
Samizdis Avatar asked Jun 08 '10 22:06

Samizdis


3 Answers

If you want and need truly OS independent, language independent inter process communication, sockets are probably the best option.

This will allow the two programs to communicate across machines, as well (without code changes).

For reading material, here's a Python Socket Programming How To.

like image 61
Reed Copsey Avatar answered Nov 20 '22 12:11

Reed Copsey


Two possibilities:

  • Use IP sockets. There are some examples in the Python docs. (Really not that hard if you just use the basic read/write stuff.) On the other hand, sockets in C are generally not that simple to use.

  • Create a third application. It launches both applications using subprocess and communicates with both applications through pipes. The chess applications must only be able to read/write to stdin/stdout.

    This has the additional benefit that this application could check if a move is legal. This helps you finding bugs and keeping the games fair.

like image 37
Georg Schölly Avatar answered Nov 20 '22 11:11

Georg Schölly


You can use Protobuf as the inter-program protocol and read/write from a file each one turns.

You may read the intermediate file every n seconds.

Once you have this working, you may move to use sockets, where each program would start a server and wait for connections.

The change should be small, because the protocol would be protobuf already. So, the only place you have to change is where you either read from a socket or from a file.

In either case you'll need an interchange protocol.

edit

Ooops I misread and I thought it was C++.

Anyway, here's the C support for protobuf but is still work in progress work

http://code.google.com/p/protobuf-c/

like image 2
OscarRyz Avatar answered Nov 20 '22 12:11

OscarRyz