4

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".

3
  • You could use try and except like described here docs.python.org/2/tutorial/errors.html
    – aronadaal
    Commented May 18, 2015 at 21:15
  • Still does not work try: grequests.map(async_list,size=10) except ConnectionError as e: print e
    – pauts
    Commented May 18, 2015 at 21:42
  • Um. what does not work? If you don't what the output then dont print it! (replace print e with pass)
    – hitzg
    Commented May 21, 2015 at 8:09

2 Answers 2

6

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

1

This has been fixed with the latest release of grequests - https://github.com/kennethreitz/grequests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.