sssbbbaaa
sssbbbaaa

Reputation: 244

Python 'subprocess' gets stuck

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.

Upvotes: 0

Views: 401

Answers (1)

CIsForCookies
CIsForCookies

Reputation: 12837

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

Upvotes: 1

Related Questions