1

As part of a larger program, I am using

a subprocess.call('some external program', shell=True)

(It is an old C program and I only have the compiled version sitting on a network server, so I can't just grab the C source code and interface it with Python)

to temporarily generate some data, which I read into Python again. The problem is that the external program called via subprocess sometimes gets stuck on certain input files. Is there a way to skip the subprocess.call(), e.g., if the shell doesn't respond after some arbitrary time limit?

E.g., 

# for data in directory:
    # do sth.
    subprocess.call('call prog.', shell=True) # skip if takes longer than 5 min
    # analyze data
  • possible duplicate of [subprocess with timeout](http://stackoverflow.com/questions/1191374/subprocess-with-timeout) – Blorgbeard May 08 '14 at 22:37

1 Answers1

0

Starting with Python 3.3 you can set the timeout argument in seconds:

try:
    return_code = subprocess.call('call prog.', shell=True, timeout=10)
except TimeoutExpired:
    # handle timeout exception here which gets throw after
    # 10 seconds in this example
Matt
  • 14,353
  • 5
  • 53
  • 65