【问题标题】:Popen communicate vs Popen stdin.write()Popen 通信与 Popen stdin.write()
【发布时间】:2015-11-19 21:39:40
【问题描述】:

我正在使用 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 going home now' 传递给java引擎运行并获取输出。

接下来它会产生“He is going home now”并重复该过程。

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

【问题讨论】:

    标签: java python subprocess


    【解决方案1】:

    p.communicate()会根据the subprocess docs等待进程终止。除非您计划在每次通信时终止并重新启动您的 java 进程,否则您可以使用第二种方法(读/写)。

    【讨论】:

      猜你喜欢
      • 2015-09-08
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多