diff --git a/octopus/core.py b/octopus/core.py index cbd5474..69bba11 100644 --- a/octopus/core.py +++ b/octopus/core.py @@ -7,6 +7,7 @@ try: import requests + import requests.exceptions except ImportError: print("Can't import requests. Probably setup.py installing package.") @@ -100,6 +101,13 @@ def do_work(self): body=str(err), error=err ) + except requests.exceptions.Timeout: + err = sys.exc_info()[1] + response = ResponseError( + status_code=500, + body=str(err), + error=err + ) if self.cache: self.response_cache.put(url, response) diff --git a/tests/test_octopus.py b/tests/test_octopus.py index 5d944c3..c01061e 100644 --- a/tests/test_octopus.py +++ b/tests/test_octopus.py @@ -181,3 +181,19 @@ def handle_url_response(url, response): expect(self.response.body).to_include("HTTPConnectionPool(host='kagdjdkjgka.fk', port=80)") expect(self.response.body).to_include('Max retries exceeded with url: /') expect(self.response.error).to_be_instance_of(requests.exceptions.ConnectionError) + + def test_can_handle_timeouts(self): + url = 'http://baidu.com' + otto = Octopus(concurrency=1, request_timeout_in_seconds=0.1) + + def handle_url_response(url, response): + self.response = response + + otto.enqueue(url, handle_url_response) + + otto.start() + + otto.wait(5) + + expect(self.response.body).to_include('Connection to baidu.com timed out') + expect(self.response.error).to_be_instance_of(requests.exceptions.Timeout)