Search code examples
pythontimersubprocessexternalpopen

Detect if external call takes to long time in Python


In my Python script I make an external call using subprocess.Popen(). If the call hasn't returned within x seconds I need to send an email indicating that something is wrong. How can I put a time limit on the execution of the external call? Note that I am not interested in terminating the call, just detecting that it takes too long.

Thanks in advance!


Solution

  • That's what the subprocess object's poll is for. See http://docs.python.org/library/subprocess.html#subprocess.Popen.poll

    You can use time.sleep() to wait for the timeout interval. Then poll the subprocess object to see if it's done.

    If it's done, that's good.

    If it's not done, your app sends an email.