[英]How to start “python.exe” as subprocess in scala?
My requirement is to form an integration between scala and python. 我的要求是在scala和python之间形成一个集成。 How do I start "python.exe" and executes python commands from scala. 如何启动“ python.exe”并从scala执行python命令。
I have tried the code snippet from following link, but it doesn't seems to be working. 我已经尝试了以下链接中的代码段,但似乎没有用。 Could you please help me this ? 你能帮我这个忙吗?
How does the “scala.sys.process” from Scala 2.9 work? Scala 2.9中的“ scala.sys.process”如何工作?
Thanks in Advance ! 提前致谢 !
The process API is quite contrived and absolutely horribly (= not) documented. 流程API是非常人为设计的,并且绝对可怕(=未记录)。 I often go back to the Java API because it makes more sense to me. 我经常回到Java API,因为它对我来说更有意义。
Here is an example. 这是一个例子。 Overwriting the os
variable to "store" the output-stream seems like an idiotic approach (in Java you can just query the output-stream from the process). 覆盖os
变量以“存储”输出流似乎是一种愚蠢的方法(在Java中,您可以只从流程中查询输出流)。 Perhaps the designers of the API can enlighten us what they were thinking. API的设计者也许可以启发我们他们的想法。 Probably there is a more elegant solution: 可能有一个更优雅的解决方案:
import sys.process._
var os: java.io.OutputStream = _
val python = Process(Seq("python","-i")).run(BasicIO.standard(os = _) /* WTF? */)
def pushLine(s: String): Unit = {
os.write(s"$s\n".getBytes("UTF-8"))
os.flush()
}
pushLine("1+1")
pushLine("exit()")
I recently had to undertake a very similar project. 我最近不得不承担一个非常相似的项目。 Rather than just executing a python script and parsing the output, i needed to start up some python code and send commands to it to execute and respond back. 我不仅需要执行python脚本并解析输出,还需要启动一些python代码并向其发送命令以执行并响应。 After some time trying to do this communication over STDIN/STDOUT, I opted for using ZeroMQ as my interop communications channel 经过一段时间尝试通过STDIN / STDOUT进行此通信后,我选择使用ZeroMQ作为我的互操作通信通道
By doing this, the execution of code became as simple as 这样,代码的执行变得非常简单
val process = s"python commandRunner.py $port".run()
where $port
is the port on which my code is listening for the python code to connect back to me via ZeroMQ, while the commandRunner
script runs a simple ZeroMQ event-loop $port
是我的代码正在侦听python代码以通过ZeroMQ连接回我的commandRunner
,而commandRunner
脚本运行一个简单的ZeroMQ事件循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.