Throttler for asyncio Python that throttles to the next whole second
Project description
aio-throttle-to-next-second
Throttler for asyncio Python that throttles to the next whole second, as reported by time.time()
. This is useful to force an order on requests to a service that uses a "latest timestamp wins" strategy, such as S3.
Installation
pip install aio-throttle-to-next-second
Usage
Create a shared Throttler
, with no arguments
from aio_throttle_to_next_second import Throttler
throttler = Throttler()
and then just before the piece(s) of code to be throttled, call this and await
its result.
await throttler()
# Each execution reaching this line will reach this line at a different second
Example: multiple tasks throttled
import asyncio
import time
from aio_throttle_to_next_second import Throttler
async def main():
throttler = Throttler()
await asyncio.gather(*[
worker(throttler) for _ in range(10)
])
async def worker(throttler):
await throttler()
# Each print will show a distinct second, though all workers started together
print(int(time.time()))
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Example: single task throttled
import asyncio
import random
import time
from aio_throttle_to_next_second import Throttler
async def main():
throttler = Throttler()
for _ in range(10):
await throttler()
# Each print will show a distinct second, though there is a random sleep
print(int(time.time()))
await asyncio.sleep(random.random())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for aio-throttle-to-next-second-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12c7f9832649a3c1b4c27c3e2641662bb5bd63724649bbbeaf1b3a2384277b95 |
|
MD5 | 8a2ca956080c04270810b600727356b5 |
|
BLAKE2b-256 | 2d928967cd162a1c97512b9b2310433bd27923c6232a1f8fe2b2cf836d89ccc3 |
Close
Hashes for aio_throttle_to_next_second-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 289d6f13c2d8e4751446af17a825b075331ddc7f2f16271ee89c643bbc78dc4f |
|
MD5 | 508d244a85a9d2f4364577f9bc0214c0 |
|
BLAKE2b-256 | a01157255c2d93a468b17fd8c1666b4df2c47363e499f9a425ae9a2e7810d5b8 |