0

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.

asked Jul 15, 2022 at 5:08

1 Answer 1

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?

  1. 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.

  1. 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...

  1. 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

answered Jul 15, 2022 at 5:15
Sign up to request clarification or add additional context in comments.

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.
true. I read the get processes' status part and skipped the (working or waiting for button clicked)
@tdelaney and is that possible ?I thought checking the process' resource consumption but that is not definite. I think stackoverflow.com/a/27869588/3512538 might be a good solution on Linux, but don't know how to get the same result on Windows

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.