Answers for "set timeout in request python"

1

python requests.get timeout

# Here is how to set a time out for requests.get in python
# its simple!
import requests

link = 'https://google.com' 
request_from_link = requests.get(link, timeout=10) 
# this causes the code to call a timeout if the connection or delays in 
# between the reads take more than 10 seconds
print(request_from_link)
Posted by: Guest on May-07-2021
2

python timeout

You may use the signal package if you are running on UNIX:

import signal

# Register an handler for the timeout
def handler(signum, frame):
  print("Forever is over!")
       raise Exception("end of time")
   

# This function *may* run for an indetermined time...
def loop_forever():
     import time
     while 1:
         print("sec")
         time.sleep(1)
         
         

# Register the signal function handler
signal.signal(signal.SIGALRM, handler)


# Define a timeout for your function
signal.alarm(10)
0

try:
    loop_forever()
except Exception, exc:
    print(exc)
Posted by: Guest on February-26-2021

Python Answers by Framework

Browse Popular Code Answers by Language