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.

sssbbbaaa
  • 204
  • 2
  • 12

1 Answers1

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

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • 1
    `proc.poll` doesn't tell you that the program is waiting for a button to be clicked, only that it hasn't exited yet. – tdelaney Jul 15 '22 at 05:20
  • true. I read the `get processes' status` part and skipped the `(working or waiting for button clicked)` – CIsForCookies Jul 15 '22 at 05:24
  • @tdelaney and is that possible ?I thought checking the process' resource consumption but that is not definite. I think https://stackoverflow.com/a/27869588/3512538 might be a good solution on Linux, but don't know how to get the same result on Windows – CIsForCookies Jul 15 '22 at 05:30