Skip to main content

Throttles for Python coroutines.

Project description

aiothrottles

GitHub license PyPI PyPI version Documentation Travis status

aiothrottles synchronization primitives are designed to be extension (along the time) to asyncio synchronization primitives.

aiothrottles has the following basic synchronization primitives:

  • Throttle

Getting started

aiothrottles requires python 3.5+. Install package using pip

pip install aiothrottles

Throttle

Implements a rate limiting for asyncio task. A throttle can be used to guarantee exclusive access to a shared resources and locks access for a given time after releasing.

The preferred way to use a Throttle is an async with statement:

throttle = Throttle('1/s')

# ... later
async with throttle:
    # access shared state

which is equivalent to:

throttle  = Throttle('1/s')

# ... later
await throttle.acquire()
try:
    # access shared state
finally:
    throttle.release()

rates

The allowed call rate is determined by the rate argument. Pass the rate in the format {integer limit}/{unit time} or {limit's numerator}/{limit's denominator}{unit time}. For, example:

  • rates with integer limits
    • 4/s, 5/m, 6/h, 7/d
    • 1/second, 2/minute, 3/hour, 4/day
  • rates with rational limits
    • 1/3s, 12/37m, 1/5h, 8/3d

Examples

decorator

Use of Throttle as decorator for coroutines:

import time
from aiothrottles import throttle  # Throttle alias

@throttle(rate='1/s')
async def foo(n):
    print(n, time.time())

for i in range(5):
    await foo(i)

# 0 1563272100.4413373
# 1 1563272101.4427333
# 2 1563272102.4441307
# 3 1563272103.445542
# 4 1563272104.4468124

awaitable

Use of aiothrottles.Throttle as awaitable object:

import time
from aiothrottles import Throttle

throttle = Throttle(rate='1/s')

async def foo(n):
    print(n, time.time())

for i in range(5):
    await throttle
    await foo(i)

# 0 1563275828.253736
# 1 1563275829.2547996
# 2 1563275830.2562528
# 3 1563275831.257302
# 4 1563275832.2587304

context manager

Use of aiothrottles.Throttle as context:

import time
from aiothrottles import Throttle

throttle = Throttle(rate='1/s')

async def foo(n):
    print(n, time.time())

for i in range(5):
    async with throttle:
        await foo(i)

# 0 1563275898.6722345
# 1 1563275899.673589
# 2 1563275900.6750457
# 3 1563275901.6763387
# 4 1563275902.6777005

License

aiothrottles is released under the BSD 3-Clause License.

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

aiothrottles-0.0.1a1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

aiothrottles-0.0.1a1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file aiothrottles-0.0.1a1.tar.gz.

File metadata

  • Download URL: aiothrottles-0.0.1a1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.5.6

File hashes

Hashes for aiothrottles-0.0.1a1.tar.gz
Algorithm Hash digest
SHA256 126b021314e9f4f76d78c831c14a78d75e2a7b17c38b74839bc7713257c01587
MD5 c5cbb6002c4076288d3590509d0d6e76
BLAKE2b-256 7819afbd6c08781e024d9e2496a93ed2849af0b4cc2ba2349f3e90b12b982a65

See more details on using hashes here.

File details

Details for the file aiothrottles-0.0.1a1-py3-none-any.whl.

File metadata

  • Download URL: aiothrottles-0.0.1a1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.5.6

File hashes

Hashes for aiothrottles-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 5923500d7391cadfe0c3d03fdc093e99112397c7442807cdd0bdeee540710126
MD5 619a059d8fa2887d1c46662bf55fd1b7
BLAKE2b-256 cef7efd5dcadcee747363b82bc36d9f8795c53bf13a41dc4c9b02746c890c02b

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