开发者

Python script executes another script and resumes after second script completes its job

开发者 https://www.devze.com 2023-03-22 11:41 出处:网络
Is there a way to check if a subprocess has finished its job? My python script executes an 123.exe from cmd. 123.exe then does some things and in the end it writes 1 or 0 to a txt file. Python script

Is there a way to check if a subprocess has finished its job? My python script executes an 123.exe from cmd. 123.exe then does some things and in the end it writes 1 or 0 to a txt file. Python script then reads the txt file and continues with the job if '1' and stops if '0'. At the moment all that I can think of, is to put the python script to sleep for a minute. That way the 123.exe has most certainly written 1 or 0 into the txt file. This realization is very simple but at the same time stupid.

So my question is, is there a way to deal with this problem without the need for timeout? A w开发者_StackOverflow中文版ay to make the python script to wait til the 123.exe stops?


Use:

retcode = subprocess.call(["123.exe"])

This will execute the command, wait until it finishes, and you get its return code into retcode (this way you could also avoid the need of checking the output status file, if the command returns a proper return code).

If you need to instantiate manually the subprocess.Popen, then go for

Popen.wait()

(and then check Popen.returncode).


I would use the call() shorthand from the subprocess module. From the documentation:

Run command with arguments. Wait for command to complete, then return the returncode attribute.

It should work this way:

import subprocess
retcode = subprocess.call(["123.exe"])

of course you won't use the retcode, but your script will anyhow hang until the 123.exe has terminated.


Try this:

p = subprocess(...)
os.waitpid(p.pid, 0)

This will wait until the process is done and your script will continue from here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号