Search code examples
pythonpython-requestsgrequests

How to handle errors in GRequests?


I have this code

#!/usr/bin/python

import grequests

urls = [
'http://google.com',
'http://doesnotexists.tld'
]


def do_something(response, **kwargs):
        print response.text

async_list = []

for u in urls:
    action_item = grequests.get(u, timeout=10, hooks = {'response' : do_something})
    async_list.append(action_item)

grequests.map(async_list,size=10)

How do I handle errors without getting usual Python error messages?
For example for domain which does not exists it prints out "not found".


Solution

  • It seems that grequests installed from pypi (with pip) doesn't include exception handling. But the version from github has implemented that feature:

    def map(requests, stream=False, size=None, exception_handler=None)
    

    So you should clone grequests or download grequests.py from github and use that version. You can directly install that version with pip:

    pip install git+https://github.com/kennethreitz/grequests.git
    

    If you're looking for exception handling examples you could have a look at the test.py in the repository. https://github.com/kennethreitz/grequests/blob/master/tests.py