简体   繁体   中英

Python subprocess module wait method

I am learning the subprocess module in python, and to my understanding, the wait method, blocks the thread from executing the rest of the code until the launched process is closed. But when I cann the wait method it still executes the rest of the code:

def startCalc():
    x = subprocess.Popen('C:\\Windows\\System32\\calc.exe')
    time.sleep(5)
    x.wait()
    print('finished waiting')
    print(x.poll())
    print(x.wait())

startCalc()

If I am not wrong, the "finished waiting statement, would not appear in the output until I close the calculator, but it does. Where am I wrong?

The problem isn't with your code, but rather with the calc.exe executable. It starts the calculator and returns immediately with 0 exit status. So, from the perspective of your program, the process ran to completion successfully. As far as I know, calc.exe doesn't have a way to launch in attached mode.

Test this by opening a powershell, or cmd terminal and launching calc.exe . You get the prompt back immediately.

I am not familiar with the ".wait" function, but if you want your code to wait for the execution of the "calc.exe" process, you could replace "Popen" with "call":

x = subprocess.call('C:\\Windows\\System32\\calc.exe')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM