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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3356554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:27:54  来源:igfitidea点击:

IPC (inter process communication) between python and java

javapythonipcmultiprocessingmultiple-processes

提问by Adam Fraser

First, a little explanation of whyI'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.

首先,解释一下为什么我首先要问这个问题:我正在编写一个 python 程序(带有 wxPython gui),它需要从 python 调用 Java AWT 程序并从中提取数据。我在 Windows 上有一个进程内工作解决方案。只要我无头运行 Java 应用程序,我在 OSX 上也有一个进程内解决方案。不幸的是,我没有找到在 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 的经验很少,因此我非常感谢任何建议,甚至是正确方向的推动。

采纳答案by Adam Fraser

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. I ended up using and modifying the TCP Java client and Python server.

本教程介绍了用 4 种语言编写的简单聊天程序的 TCP 和 UDP 变体。我最终使用并修改了 TCP Java 客户端和 Python 服务器。

回答by Kenn

This is the opensource solution Google uses to do IPC between Java and Python. https://code.google.com/p/protobuf/

这是 Google 用来在 Java 和 Python 之间进行 IPC 的开源解决方案。 https://code.google.com/p/protobuf/

Recommended.

受到推崇的。

回答by Jeremy Brown

回答by Vebjorn Ljosa

Use subprocess.Popento 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!).

使用subprocess.Popen启动 Java 进程并建立与之通信的管道。要以语言中立、平台中立、可扩展的方式有效地序列化和反序列化数据,请查看协议缓冲区(由Jon Skeet 提供!)。

回答by wandermonk

I had a similar situation where I had to communicate between a Java process and a Linux process. I used named pipes.

我遇到过类似的情况,我不得不在 Java 进程和 Linux 进程之间进行通信。我使用了命名管道。

Try mkfifo() implementation in python.

在 python 中尝试 mkfifo() 实现。

回答by Mr. A

IPC using subprocessfrom in python

subprocess在 python 中使用from 的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()