Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 669
pauts Avatar asked Oct 17 '25 11:10

pauts


2 Answers

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

like image 188
aronadaal Avatar answered Oct 18 '25 23:10

aronadaal


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

like image 20
Saurabh Hirani Avatar answered Oct 19 '25 00:10

Saurabh Hirani