Popen 通信 vs Popen stdin.write() [英] Popen communicate vs Popen stdin.write()

查看:31
本文介绍了Popen 通信 vs Popen stdin.write()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 引擎来处理一些内容.

I am using a java engine to process some content.

我想知道它会不会像这样占用不必要的资源.

I am wondering will it take up unnecessary resources like this.

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 应用程序.

To give some context to the problem. The strings are generated on the fly and hence i do not want to open and close the java application everything a string is generated.

例如:

我的python引擎创建了一个字符串

My python engine creates a string

'我现在要回家了' 传递给java引擎运行并获取输出.

'Hi i am going home now' which is passed to the java engine to run and obtains the output.

接下来它会产生He is going home now",然后重复这个过程.

Next it produces 'He is going home now' and the process repeats.

显然java程序写入System.out.println(output);

推荐答案

p.communicate() 会根据 子流程文档.除非您打算在每次通信时终止并重新启动 Java 进程,否则您只能使用第二种方法(读/写).

p.communicate() will wait for the process to terminate according to the subprocess docs. Unless you plan to terminate and restart your java process each time you communicate, the second method (read/write) is all you have available.

这篇关于Popen 通信 vs Popen stdin.write()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆