I use 'subprocess' to do work repeatedly like below.
import subprocess
arg_list = ['a', 'b', 'c', 'd']
for arg in arg_list:
proc = subprocess.Popen(['some_app.exe', arg])
proc.communicate()
Most of runs terminates successfully but occasionally the app requires to click the 'OK' button. Then the script gets stuck there. A working time varies very widely depending on arguments('a', 'b', ...) so that I cannot set proper a timeout.
Is there any method to get processes' status(working or waiting for button clicked)? If I can tell the app is waiting for clicking then can terminate the process.
Here is a similar question but no solution yet.
1 Answer 1
Is there any method to get processes' status(working or waiting for button clicked)? If I can tell the app is waiting for clicking then can terminate the process.
Not that I'm aware of. You can only check the process' running status using proc.poll(). However, proc.communicate() is blocking, so you'll have to poll it in another thread.
How to solve it?
- Kill after timeout
Note that proc.communicate can get a timeout argument, so although A working time varies very widely depending on arguments, if you decide to calculate it - you can pass it and subprocess will kill the process for you after timeout.
- Spam "enter"
I suggest you look here on how to send keyboard events. You might just want to add another thread that sends enter to your process every N seconds...
- optional (didn't check it) psutil.status
Try using psutil.status() status options to check if your process is maybe psutil.STATUS_SLEEPING. Not sure how things work in Windows. If you were running on Linux I assume your process, if waiting for a keyboard key, would be psutil.STATUS_IDLE or psutil.STATUS_WAITING
3 Comments
proc.poll doesn't tell you that the program is waiting for a button to be clicked, only that it hasn't exited yet.get processes' status part and skipped the (working or waiting for button clicked)