closeio / closeio-api

Python API Client for Close
http://developer.close.com/
MIT License
65 stars 47 forks source link

Async request error handling #7

Closed pirate closed 7 years ago

pirate commented 9 years ago

Hey close.io devs, this is Nick Sweeting with drchrono, thanks for writing this python library, it makes our lives a lot easier.

I'm writing a PR for the TODO you guys put in init.py:71
grequests now supports passing an exception_handler function so we no longer have to deal with this APIError() monkey business. I've started updating the code and should have a pull request ready in a few days.

the new code will follow this pattern:

def map(self, reqs, max_retries=None):
    import grequests
    max_retries = max_retries or self.max_retries

    retries = 0
    failed = []
    responses = grequests.map(reqs, exception_handler=lambda req, excp: failed.append(req))

    while failed and retries < self.max_retries:
        succeeded = grequests.map(failed, exception_handler=lambda req, excp: pass)       # leave the ones that continue failing in failed
        failed = [ req for req in failed if req not in reversed(succeeded) ]              # checking if reversed succeeded contains our request is faster because we append good ones to the end
        responses += succeeded
        retries += 1
        time.sleep(2)

    return [resp.json() for resp in responses]
anemitz commented 9 years ago

Hey Nick, That PR was closed in lieu of https://github.com/kennethreitz/grequests/pull/22 which looks like it's stagnant. The discuss seems lead us to instead using https://github.com/ross/requests-futures which supports error handling.

wojcikstefan commented 7 years ago

Hi Nick, we decided to move away from grequests in v1.0 (you can see some arguments for it in https://github.com/closeio/closeio-api/pull/78#issuecomment-275871159 and in https://github.com/closeio/closeio-api/pull/78#issuecomment-276209971).

To replicate a similar behavior, you can spawn multiple api.get/post/put/delete requests in a gevent.pool.