将数据从Python程序发送到Java程序 [英] Send data from Python program to Java program
问题描述
我有一个Python程序 status.py
,该程序将一些状态更新(字符串)打印到控制台。但是,我需要编写一个JavaFX程序 Display.java
,该程序将启动Python程序,然后将Python程序的状态更新打印到 TextArea
。
I have a Python program status.py
that prints some status updates (strings) to the console. However, I need to write a JavaFX program Display.java
that launches the Python program and then prints the Python program's status updates to a TextArea
.
要完成此操作,我更改了 status.py
,将其状态更新写入文本文件,然后让Java程序将文本文件读取到 TextArea
中。
To accomplish this I've altered status.py
to write its status updates to a text file and then have the Java program read the text file into the TextArea
.
文本文件方法有效,但我觉得有更好的通信方式。我正在考虑让Python程序将其状态更新写入套接字,并让Java程序使用 ServerSocket
监听套接字。
The text file method is working, but I feel like there is a better way to communicate. I am thinking about having the Python program write its status updates to a socket and having the Java program listen to the socket using ServerSocket
.
正在使用一种网络方法,例如通过套接字进行通信,这是完成 status.py
与 Display.java
?如果不是,那么完成这种进程间通信的最佳方法是什么?
Is using a networking approach such as communicating through sockets the best way to accomplish communication between status.py
and Display.java
? If not, what is the best way to accomplish such inter-proccess communication?
推荐答案
有两种方法可以从Python进程转换为Java进程:
There are two options for communicating information from a Python process to a Java process:
- Java流行的网络库将使通过套接字轻松读取数据变得容易。由于您可以轻松地在Python中打开套接字,因此实现套接字通信将是最简单的解决方案。
- 如果您具有 Jython ,您可以通过将Python代码包含在Java代码中来消除执行进程间通信的需要。
- Java's popular networking libraries will make it easy to read in data through a socket. Since you can easily open up a socket in Python, implementing socket communication would be the easiest solution.
- If you have experience with Jython, you can eliminate the need to perform inter-process communication by including the Python code within the Java code.
这篇关于将数据从Python程序发送到Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!