Skip to main content

Throttler for asyncio Python that throttles to the next whole second

Project description

aio-throttle-to-next-second CircleCI Test Coverage

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


Download files

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

Source Distribution

aio-throttle-to-next-second-0.0.1.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file aio-throttle-to-next-second-0.0.1.tar.gz.

File metadata

  • Download URL: aio-throttle-to-next-second-0.0.1.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for aio-throttle-to-next-second-0.0.1.tar.gz
Algorithm Hash digest
SHA256 12c7f9832649a3c1b4c27c3e2641662bb5bd63724649bbbeaf1b3a2384277b95
MD5 8a2ca956080c04270810b600727356b5
BLAKE2b-256 2d928967cd162a1c97512b9b2310433bd27923c6232a1f8fe2b2cf836d89ccc3

See more details on using hashes here.

File details

Details for the file aio_throttle_to_next_second-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: aio_throttle_to_next_second-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for aio_throttle_to_next_second-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 289d6f13c2d8e4751446af17a825b075331ddc7f2f16271ee89c643bbc78dc4f
MD5 508d244a85a9d2f4364577f9bc0214c0
BLAKE2b-256 a01157255c2d93a468b17fd8c1666b4df2c47363e499f9a425ae9a2e7810d5b8

See more details on using hashes here.

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