spyoungtech / grequests

Requests + Gevent = <3
https://pypi.python.org/pypi/grequests
BSD 2-Clause "Simplified" License
4.49k stars 331 forks source link

i need "exception_handler" get back to the main thread #78

Closed coos closed 8 years ago

coos commented 8 years ago

eg.

def map(requests, stream=False, size=None, exception_handler=None):
    ……
    ret = []

    for request in requests:
        if request.response:
            ret.append(request.response)
        elif exception_handler:
            #exception_handlerrequest, request.exception)
            # new code:
            ret.append(exception_handler(request))

    return ret

And then I can use it like this:

from requests import Response
def timeOutException(request):
    if 'ok' in dir(request.response):
        return request.response
    else:
        # build respone for timeout
        response = Response()
        response.status_code = 444
        response.url = request.url
        return response
In [119]: res = grequests.map(apprs, exception_handler=timeOutException)                

In [120]: res
Out[120]: [<Response [200]>, <Response [444]>, <Response [404]>, <Response [200]>]

The same process can also be in imap.

rtdean commented 8 years ago

Take a look at #58 I believe this does what you are looking for.