简体   繁体   English

python和java之间的IPC(进程间通信)

[英]IPC (inter process communication) between python and java

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.首先,解释一下为什么我首先要问这个问题:我正在编写一个 python 程序(带有 wxPython gui),它需要从 python 调用 Java AWT 程序并从中提取数据。 I have an in-process working solution on Windows.我在 Windows 上有一个进程内工作解决方案。 I also have an in-process solution on OSX so long as I run the Java app headless.只要我无头运行 Java 应用程序,我在 OSX 上也有一个进程内解决方案。 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.不幸的是,我没有找到在 OSX 上的同一进程中运行两个 GUI 的合理解决方案,因为 AWT 和 WX 都想要第一个线程并且不能共享 wx 消息循环。

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.我想做的是在与我的 Python 程序不同的进程中启动一个 Java 程序,并建立一个管道或队列或用于来回传递数据(特别是字节数组)的东西。

I'd greatly appreciate any suggestions, or even a nudge in the right direction as I have very little experience with IPC.由于我对 IPC 的经验很少,我非常感谢任何建议,甚至是朝着正确方向的推动。

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: https://web.archive.org/web/20080913064702/http://www.prasannatech.net/2008/07/socket-programming-tutorial.html研究套接字编程,我在这里找到了一个很棒的资源: https : //web.archive.org/web/20080913064702/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.本教程介绍了用 4 种语言编写的简单聊天程序的 TCP 和 UDP 变体。 I ended up using and modifying the TCP Java client and Python server.我最终使用并修改了 TCP Java 客户端和 Python 服务器。

This is the opensource solution Google uses to do IPC between Java and Python.这是 Google 用来在 Java 和 Python 之间进行 IPC 的开源解决方案。 https://code.google.com/p/protobuf/ https://code.google.com/p/protobuf/

Recommended.受到推崇的。

Use subprocess.Popen to start the Java process and establish pipes to communicate with it.使用subprocess.Popen启动 Java 进程并建立与之通信的管道。 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 !).要以语言中立、平台中立、可扩展的方式有效地序列化和反序列化数据,请查看协议缓冲区(由Jon Skeet 提供!)。

I had a similar situation where I had to communicate between a Java process and a Linux process.我遇到过类似的情况,我必须在 Java 进程和 Linux 进程之间进行通信。 I used named pipes.我使用了命名管道。

Try mkfifo() implementation in python.在 python 中尝试 mkfifo() 实现。

IPC using subprocess from in python使用python中的subprocess IPC

IPC.java file here java code will receive number and send square of it.此处的IPC.java 文件java 代码将接收数字并发送它的平方。

import java.util.Scanner;

public class IPC {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String data="";
        while(scanner.hasNext()){
            // Receive data from Python code 
            data = scanner.nextLine();

            // Process data (calculate square)
            int x = Integer.parseInt(data);
            int square = x*x;


            // Send data to python code
            System.out.println(square);
        }
        scanner.close();
    }
}

IPC.py file IPC.py 文件

import subprocess
subprocess.run(["javac", "IPC.java"])
proc = subprocess.Popen(["java", "IPC"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for i in range(10):
    # Send to java code
    proc.stdin.write(b'%d\n' % i)
    proc.stdin.flush()
    proc.stdout.flush()

    # Receive data from java code
    output = proc.stdout.readline()
    print (output.rstrip())
proc.communicate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM