[英]Python script not continuing after calling subprocess
I have a python script calling a java file with the subprocess
module: 我有一个python脚本通过
subprocess
模块调用java文件:
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"
This works correctly, and the java file then waits for the python script to continue and connect a socket, as I want it to. 这可以正常工作,然后Java文件等待python脚本继续运行并连接套接字,如我所愿。 But the python script doesn't continue.
但是python脚本不会继续。 Why not?
为什么不? I know it doesn't because the
print
statement never executes. 我知道不是因为
print
语句从不执行。
Everything runs as expected when I manually run the java file from Eclipse and then execute the python script from the command line. 当我从Eclipse手动运行java文件,然后从命令行执行python脚本时,一切都会按预期运行。 I also tried with
subprocess.Popen()
instead of subprocess.call()
, with no difference in the outcome. 我还尝试了
subprocess.Popen()
而不是subprocess.call()
,结果没有差异。
Thank you @KSFT; 谢谢@KSFT;
subprocess.call()
doesn't return until the command finishes, but subprocess.Popen()
does. 直到命令完成,
subprocess.call()
才返回,但是subprocess.Popen()
会返回。 So I made the call with subprocess.Popen()
and then time.sleep(0.5)
. 所以我先用
subprocess.Popen()
然后再用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. 让python脚本等待0.5秒可以使java文件有足够的时间打开和初始化套接字连接。
subprocess.call()
doesn't return until the command it runs finishes. subprocess.call()
直到其运行的命令完成才返回。 You can use multithreading to run something else at the same time. 您可以使用多线程同时运行其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.