英文:
How to pass some data from python3 to java
问题
这是我的问题:我需要创建一个 Discord 机器人,使用语音识别来识别语音命令,并将识别出的文本传送到一个 Java 程序(在这种情况下是一个 Spigot Minecraft 插件)中,这个程序从机器人/Python 程序那里接收。我已经搜索了很多地方,但没有找到合适的方法来实现这一点。我唯一能想到的是创建一个包含必要数据或其名称的文件,并将其用作机器人和插件之间的“桥梁”,但我认为这不是一种非常正统或合适的方法。(我尝试过 Jython,但据我所知它仍停留在 Python 2.7)。
英文:
Here is my problem: I need to create a discord bot that uses speech recognition to recognize voice commands and send the recognized strings to a Java program (a Spigot Minecraft plugin in this case) from that bot/python program. I've scanned all over and I couldn't find any suitable methods to do this. The only thing that I can think of is creating a file with the necessary data inside it or its name and use that as a "bridge" between the bot and the plugin, but I don't think it's a very orthodox or suitable method. (I tried jython, but it's stuck to python2.7 as far as I know).
答案1
得分: 2
我假设我们正在讨论独立的Python和Java进程,因为你发现Jython对你的情况没有用。我认为这实际上有两个问题:(a) 如何将数据从Python传递到Java,以及(b) 使用什么格式。
对于问题(a),你可以使用命名管道或域套接字(我相信它们现在也存在于Windows上]3)。关于这两种进程间通信抽象之间的区别,可以参考这里。另一个选择是TCP,但除非有可能的情况是进程位于不同的计算机上,否则这可能不是最佳选择。
问题(b)的答案在一定程度上取决于数据的性质。JSON可能是一个选项,但你可能还想看看可能更快的替代方案。我脑海中浮现的是Apache Thrift和Google的Protocol Buffers。
英文:
I assume we are talking about separate Python and Java processes since you found Jython to be not useful for your case. I think there are really two questions in this: (a) how to get data from Python to Java and (b) what format to use.
For (a) you could use a named pipe or domain sockets (I believe they now also exist on Windows). For the differences between these two inter-process communication abstractions see here. Another option would be TCP but unless there is a possible scenario where the processes reside on different machines this is not likely to be the best.
The answer to (b) depends a bit on the nature of the data. JSON could be an option but you might also want to look at alternatives that might be faster. The ones that come to my mind are Apache Thrift and Google's Protocol Buffers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论