Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
timeout value in subprocess
#1
Hi,

The timeout works a treat if I hard code a value as such:
try: subprocess.run(['notepad.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=2222) except: print("likely timeout.... Error: ", sys.exc_info()[0])
I'm writing a script for CI/CD testing and depending on my application, I may want it to time out after 5 seconds, or possibly even 15 minutes.  This will all get controlled by my own config file.

I'd like to do something along the lines of:
strTimeoutDuration = "5" #5 seconds.  This obtained previously in my script but hard coding for this example.
strTimeout = "timeout=" + strTimeoutDuration
subprocess.run(['notepad.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, strTimeout)
Is this feasible?  Last thing I want is to wait an upper limit of say 15 minutes for all applications when I know it was hanging after 10 seconds of not finishing.

Many thanks,
​​​​​​​J
Reply
#2
Right, got there in the end.  Still very new to Python so getting my head around syntax and understanding the help files.

In case anyone was wondering,
strTimeoutDuration = 5 #5 seconds.  This obtained previously in my script but hard coding for this example.
strTimeout = "timeout=" + strTimeoutDuration
subprocess.run(['notepad.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=strTimeoutDuration)

Cheers,
J
Reply
#3
Timeout is a parameter in the function call,just set time there.
from subprocess import check_output

out = check_output(['ping', 'google.com'], timeout=5)
print(out.decode('utf-8').strip())
Output:
Pinging google.com [172.217.22.174] with 32 bytes of data: Reply from 172.217.22.174: bytes=32 time=26ms TTL=56 Reply from 172.217.22.174: bytes=32 time=22ms TTL=56 Reply from 172.217.22.174: bytes=32 time=23ms TTL=56 Reply from 172.217.22.174: bytes=32 time=25ms TTL=56 Ping statistics for 172.217.22.174:     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds:     Minimum = 22ms, Maximum = 26ms, Average = 24ms
Here it goes fine because the process finish before 5-sec.
from subprocess import check_output
import subprocess

out = check_output(['ping', '-n', '10', 'google.com'], timeout=5)
print(out.decode('utf-8').strip())
Error:
Traceback (most recent call last):   File "C:\Python36\lib\subprocess.py", line 405, in run   ........ subprocess.TimeoutExpired: Command '['ping', '-n', '10', 'google.com']' timed out after 5 seconds
Here it use longer than 5-sec,and get a subprocess.TimeoutExpired,
This error be catch an give a better message.
from subprocess import check_output

try:
    out = check_output(['ping', '-n', '10', 'google.com'], timeout=5)
    print(out.decode('utf-8').strip())
except subprocess.TimeoutExpired:
    print('Process ran too long and got timed out')
Output:
Process ran too long and got timed out
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FTp timeout except korenron 2 3,572 Feb-01-2022, 06:51 AM
Last Post: korenron
  proc communicate not exiting on python subprocess timeout leoonardoo 0 3,783 Sep-13-2017, 09:54 AM
Last Post: leoonardoo
  AsyncSSH and timeout Standard_user 1 5,485 Nov-03-2016, 06:05 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020