9

My problem is very simple. I have a try/except code. In the try I have some http requests attempts and in the except I have several ways to deal with the exceptions I'm getting.

Now I want to add a time parameter to my code. Which means the try will only last for 'n' seconds. otherwise catch it with except.

In free language it would appear as:

try for n seconds:
    doSomthing()
except (after n seconds):
    handleException()

this is mid-code. Not a function. and I have to catch the timeout and handle it. I cannot just continue the code.

        while (recoveryTimes > 0):
            try (for 10 seconds):

                urllib2.urlopen(req)
                response = urllib2.urlopen(req)     
                the_page = response.read()
                recoveryTimes = 0

            except (urllib2.URLError, httplib.BadStatusLine) as e:
                print str(e.__unicode__())
                print sys.exc_info()[0]
                recoveryTimes -= 1

                if (recoveryTimes > 0):
                    print "Retrying request. Requests left %s" %recoveryTimes
                    continue
                else:
                    print "Giving up request, changing proxy."
                    setUrllib2Proxy()
                    break
            except (timedout, 10 seconds has passed)
                setUrllib2Proxy()
                break

The solution I need is for the try (for 10 seconds) and the except (timeout, after 10 seconds)

6
  • Are you looking for HTTPREQUEST timeout ?If yes things can be handled gracefully Commented May 28, 2015 at 13:04
  • 1
    possible duplicate of Timeout on a Python function call Commented May 28, 2015 at 13:04
  • Which framework do you use to handle the http requests? Commented May 28, 2015 at 13:07
  • I've tried catching httprequest timeout. it didnt catch it. So I just want to force a stop after 'n' seconds. I saw some multithreads solutions but its quite dont fit here. I dont want to run other code while waiting. I just want to try the code for 'n' seconds and then catching if it fails due to time. Commented May 28, 2015 at 13:07
  • @Eran Moshe are you using requests module? Commented May 28, 2015 at 13:12

2 Answers 2

7

Check the documentation

import urllib2
request = urllib2.Request('http://www.yoursite.com')
try:
    response = urllib2.urlopen(request, timeout=4)
    content = response.read()
except urllib2.URLError, e:
    print e

If you want to catch more specific errors check this post

or alternatively for requests

import requests
try:
    r = requests.get(url,timeout=4)
except requests.exceptions.Timeout as e:
    # Maybe set up for a retry
    print e

except requests.exceptions.RequestException as e:
    print e

More about exceptions while using requests can be found in docs or in this post

Sign up to request clarification or add additional context in comments.

Comments

5

A generic solution if you are using UNIX:

import time as time
import signal

#Close session
def handler(signum, frame):
    print 1
    raise Exception('Action took too much time')


signal.signal(signal.SIGALRM, handler)
signal.alarm(3) #Set the parameter to the amount of seconds you want to wait

try:
    #RUN CODE HERE

    for i in range(0,5):
        time.sleep(1)
except:
    print 2

signal.alarm(10) #Resets the alarm to 10 new seconds
signal.alarm(0) #Disables the alarm 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.