Skip to main content

A more efficient way of conforming to limits via waiting

Project description

waiter.py

PyPI PyPI - Downloads

A more efficient way of conforming to limits via waiting


Think of waiter as Python's built-in sleep() function but instead of sleeping every time it is called, it will only sleep if it needs to. This makes it easier to conform to known rate limits while avoiding unnecessary sleeps.

Let's assume you are consuming a REST API that has a rate limit of 2 requests per second. Naturally you could simply sleep for 500ms after each request as to not hit this limit. However what if you make one request, sleep for 500ms, do something else that takes longer than 500ms, make another request, and then sleep once more for 500ms? The last sleep would be unnecessary.

waiter lets you create functions that will only sleep when needed:

from waiter import create_waiter

wait = create_waiter(500)

make_quick_request()  # takes 10ms
wait()  # sleeps for 490ms
make_slow_request()  # takes 1000ms
wait()  # does not sleep

You can also decorate functions:

from waiter.decorator import will_wait

@will_wait(500)
def my_func():
    #  ...

#  You know the drill

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

waiter.py-1.0.0.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

waiter.py-1.0.0-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page