python - Handling (queuing) requests to a web service which calls rate limited external API -


i have web service exposed using flask framework.

this service uses external api, has limitation on number of times should called per second.

in normal scenario, multiple calls api leads multiple threads getting generated , external api getting called without control on number of requests per second.

is there way in can queue requests web service , call external api in throttled way.

any other ideas welcomed.


edit:

  1. i know rate external api ( 1 request per second)

  2. i ok if client requesting api has wait little long (few seconds / minutes depending on load have) before results.

  3. i don't want api clients failed results. i.e. don't want them call again , again. if external api being accessed me @ max rate possible, requests api should queued , processed when rate comes down.

  4. i read celery , redis. able queue web service calls in these queues , process them later ?

one way wrap request, rate limit failures result in exponential backoff until acceptable rate found.

in example below, keep retrying request until succeeds, waiting longer , longer between requests each time fails, maximum number of allowed retries (n_max). number of seconds waits retry request increases exponentially (1, 2, 4, 8, 16, 32, etc).

here example uses requests. specifics of catching errors , recognizing rate limit errors depend on library using make requests , type of errors external api returns, backoff algorithm should same.

def call_backoff(request_func, n_max=10):     n = 0     while n < n_max:         error = false         try:             response = request_func()         except errors.httperror e:             # can test here if rate error             # , not other error.  can decide how handle             # other errors.             error = true             if not_a_rate_error:                 raise           if response.status_code == 429:              error = true           if not error:              return response           milli = random.randint(1, 1000)          secs = (2 ** n) + milli / 1000.0          time.sleep(secs)          n += 1      # can raise error here if you'd     return none 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -