Skip to main content

aiothrottler CircleCI Test Coverage

Throttler for asyncio Python

Installation

pip install aiothrottler

Usage

Create a shared Throttler, passing a minimum interval, e.g. 0.5 seconds

from aiothrottler import Throttler

throttler = Throttler(min_interval=0.5)

and then just before the piece(s) of code to be throttled, call this and await its result.

await throttler()
# There will be a gap of at least 0.5 seconds
# between executions reaching this line

Example: multiple tasks throttled

import asyncio
import time

from aiothrottler import Throttler

async def main():
    throttler = Throttler(min_interval=0.5)
    await asyncio.gather(*[
        worker(throttler) for _ in range(10)
    ])

async def worker(throttler):
    await throttler()
    # Interval of at least 0.5 seconds between prints
    # even though all workers started together
    print(time.time())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

Example: single task throttled/smoothed

import asyncio
import random
import time

from aiothrottler import Throttler

async def main():
    throttler = Throttler(min_interval=0.5)
    for _ in range(10):
        await throttler()
        # Interval of at least 0.5 seconds between prints
        # even though each sleep is random
        print(time.time())
        await asyncio.sleep(random.random())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

Differences to alternatives

  • The API features a function to call to await its result [some use a context manager]

  • The API is imperative [some use a functional approach/higher-order function]

  • No polling is used [some use polling internally]

  • A minimum interval between resolutions is used to throttle [rather that a max resolutions per time interval, which can cause an irregular pattern of resolutions]

  • The tests cover edge cases, such as asserting on throttling after tasks being throttled have been cancelled [some alternatives do not]

Metadata

Release files for aiothrottler 0.0.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiothrottler 0.0.16
File Size Uploaded
aiothrottler-0.0.16.tar.gz 2.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiothrottler 0.0.16
File Interpreter ABI Platform
aiothrottler-0.0.16-py3-none-any.whl Python 3 none any Details

Total release size: 5.9 kB

Release files / aiothrottler-0.0.16.tar.gz

Download URL aiothrottler-0.0.16.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ab5572d88ffc4b985068439f6f64677f3f7013fbf1afcd32a4196570aad2e7c1
BLAKE2b-256 checksum
How to use checksums
f0d85af5483010313a0671956d2f0b634b8f02a7b03b7c3c96a2842cd462f0c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

Release files / aiothrottler-0.0.16-py3-none-any.whl

Download URL aiothrottler-0.0.16-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5f4c42c023c7f19619bd9331144ab060f34836be195b92e9281172345a294d7
BLAKE2b-256 checksum
How to use checksums
a72d81f6a068085d25c392ed485fda4d8579ee3052c24e0ec47fc6cbb09d3a59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.0.16 This release

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page