views:

168

answers:

3

Hello,

First, a little explanation of why I'm asking this question in the first place: I'm writing a python program (with a wxPython gui) that needs to call a Java AWT program from python and extract data from it. I have an in-process working solution on Windows. I also have an in-process solution on OSX so long as I run the Java app headless. Unfortunately there is no reasonable solution that I have found for running both GUIs within the same process on OSX because both AWT and WX both want the first thread and cannot share the wx message loop.

What I would like to do is to launch a Java program in a separate process from my Python program and establish a pipe or queue or something for passing data (specifically byte arrays) back and forth.

I'd greatly appreciate any suggestions, or even a nudge in the right direction as I have very little experience with IPC.

+1  A: 

Use subprocess.Popen to start the Java process and establish pipes to communicate with it. For serializing and deserializing data efficiently in a language-neutral, platform-neutral, extensible way, take a look at Protocol Buffers (contributed to by Jon Skeet!).

Vebjorn Ljosa
+2  A: 

Named pipes could be the answer for you. See: Create a temporary FIFO (named pipe) in Python?

Jeremy Brown
Hrm, this seems easy from the Python angle, but from what I'm reading Java will only be able to treat the FIFOs as files and won't be able to create them in an OS-independent way. This should be fine for sending images to Java, but I'm not sure if it will suffice for getting them back.
Adam Fraser
FIFOs are platform-dependent and they are in fact used via file interfaces in UNIX (http://en.wikipedia.org/wiki/Named_pipe). If you're uncomfortable with that (or prefer something supported by standard libraries), you could just use a socket.
Jeremy Brown
A: 

I attempted to code a solution using pipes but it seems that they just aren't well suited to sending multiple messages back and forth with potentially large data attached. Rather, they seem ideal for opening a "worker" style program that runs, responds, and dies.

Looking into socket programming, I found a fantastic resource here: http://www.prasannatech.net/2008/07/socket-programming-tutorial.html

The tutorial presents TCP and UDP variants of a simple chat program written in 4 languages. I ended up using and modifying the TCP Java client and Python server.

Adam Fraser