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

Anthon
  • 69,918
  • 32
  • 186
  • 246
pauts
  • 119
  • 1
  • 8
  • You could use try and except like described here https://docs.python.org/2/tutorial/errors.html – aronadaal May 18 '15 at 21:15
  • Still does not work `try: grequests.map(async_list,size=10) except ConnectionError as e: print e` – pauts May 18 '15 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 May 21 '15 at 08:09

2 Answers2

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

aronadaal
  • 9,083
  • 1
  • 19
  • 33
1

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

Saurabh Hirani
  • 1,198
  • 14
  • 21