Skip to content

depthbomb/waiter.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A more efficient way of conforming to limits via waiting

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages