Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Handling with requests_futures in python

I am trying to use requests_futures (https://github.com/ross/requests-futures) for asynchronous requests which seems to work fine. The only problem is, it doesn't throw any exceptions for me (i.e. TimeOut Exception). The code I used is:

from concurrent.futures import ThreadPoolExecutor
from requests_futures.sessions import FuturesSession

session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10))

def callback(sess, resp):
    # Print the ip address in callback
    print 'IP', resp.text

proxy = {'http': 'http://176.194.189.57:8080'}

try:
    future = session.get('http://api.ipify.org', background_callback=callback, timeout=5, proxies=proxy)
except Exception as e:
    print "Error %s" % e

#  future2 = session.get('http://api.ipify.org', background_callback=callback, timeout=5)

The first session.get() should throw an Exception as it isn't a valid proxy.

like image 975
whiterock Avatar asked Jun 20 '26 09:06

whiterock


1 Answers

For the exception to be raised, you have to check the result() method of the future object you just created.

like image 57
Rodrigo Deodoro Avatar answered Jun 21 '26 23:06

Rodrigo Deodoro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!