我正在使用java引擎来处理一些内容。
我想知道这样会占用不必要的资源吗?
command = 'some command to run my jar file'
p = subprocess.Popen(command, stdout = subprocess.PIPE, stdin = subprocess.PIPE)
比较
output,err = p.communicate('some string that is passed to the java jar')
对比
p.stdin.write('some string that is passed to the java jar \n')
p.stdin.flush()
output = p.stdout.readline()
提供问题的一些背景信息。字符串是动态生成的,因此我不想打开和关闭生成字符串的所有 Java 应用程序。
例如:
我的Python引擎创建一个字符串
'Hi i am go home now
' 传递给 java 引擎运行并获取输出。
接下来它会生成“他现在要回家了
”,并且重复该过程。
显然java程序写入System.out.println(output);
最佳答案
p.communicate()
将等待进程根据 the subprocess docs 终止。除非您打算在每次通信时终止并重新启动 java 进程,否则您只能使用第二种方法(读/写)。
关于java - Popen 通信与 Popen stdin.write(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32216622/