调用子进程后Python脚本不继续

问题描述:

我有一个用 subprocess 模块调用 java 文件的 python 脚本:

I have a python script calling a java file with the subprocess module:

import subprocess
java_cmd = ['java', '-cp', 'bin/stuff/:lib/asm-all-3.3.jar:lib/jdom.jar',
      'ch.idsia.scenarios.Main']
subprocess.call(java_cmd, shell=False)
print "Hello world"

这工作正常,然后 java 文件等待 python 脚本继续并连接套接字,正如我想要的那样.但是python脚本不会继续.为什么不?我知道它不会因为 print 语句永远不会执行.

This works correctly, and the java file then waits for the python script to continue and connect a socket, as I want it to. But the python script doesn't continue. Why not? I know it doesn't because the print statement never executes.

当我从 Eclipse 手动运行 java 文件然后从命令行执行 python 脚本时,一切都按预期运行.我还尝试使用 subprocess.Popen() 而不是 subprocess.call(),结果没有区别.

Everything runs as expected when I manually run the java file from Eclipse and then execute the python script from the command line. I also tried with subprocess.Popen() instead of subprocess.call(), with no difference in the outcome.

谢谢@KSFT;subprocess.call() 在命令完成之前不会返回,但是 subprocess.Popen() 会返回.所以我用 subprocess.Popen()time.sleep(0.5) 进行了调用.让 python 脚本等待 0.5 秒可以让 java 文件有足够的时间来打开和初始化套接字连接.

Thank you @KSFT; subprocess.call() doesn't return until the command finishes, but subprocess.Popen() does. So I made the call with subprocess.Popen() and then time.sleep(0.5). Having the python script wait 0.5 seconds allows the java file enough time to open and initialize the socket connections.