简体   繁体   English

调用子流程后,Python脚本未继续

[英]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.

相关问题 Android Studio:在活动中调用“finish()”后,前一屏幕中的代码无法继续 - Android Studio: Code in previous screen not continuing after calling “finish()” in activity 当Python脚本使用子进程运行Java程序时,需要在Java错误后继续操作 - Need to move on after java error when Python script uses subprocess to run a java program 从java运行python脚本作为子进程 - Run python script from java as subprocess 睡眠后继续线程 - Continuing a Thread After Sleeping 为什么在调用InflateObject.inflate(byte [] array)之后我的方法没有继续? - Why is my method not continuing after calling InflateObject.inflate(byte[] array)? 在python中将Java程序作为子进程调用时如何传递字典? - How to pass a dictionary when calling a java program as subprocess in python? 使用Python中的“subprocess”调用Java应用程序并阅读Java app输出 - Calling Java app with “subprocess” from Python and reading the Java app output JOptionPane之后的Java连续代码 - Java Continuing code after JOptionPane 发现异常后继续执行 - Continuing Execution after Exception found 从另一个Python脚本调用的Python脚本作为子进程执行.jar文件时出错 - Error in executing .jar file from a Python script called from another Python script, as a subprocess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM