All Projects → hallazzang → asyncio-throttle

hallazzang / asyncio-throttle

Licence: MIT license
Simple, easy-to-use throttler for asyncio.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to asyncio-throttle

adaptive throttler
manages multiple throttlers with ability to ramp up and down
Stars: ✭ 31 (-67.37%)
Mutual labels:  throttle, throttler
defense
🔮 A Crystal HTTP handler for throttling, blocking and tracking malicious requests.
Stars: ✭ 51 (-46.32%)
Mutual labels:  throttle, throttling
rush
rush.readthedocs.io/en/latest/
Stars: ✭ 42 (-55.79%)
Mutual labels:  throttle, throttler
sniffio
Sniff out which async library your code is running under
Stars: ✭ 75 (-21.05%)
Mutual labels:  asyncio
antirobot aiogram
Телеграм бот для блокировки спама
Stars: ✭ 26 (-72.63%)
Mutual labels:  asyncio
pytest-aiohttp
pytest plugin for aiohttp support
Stars: ✭ 110 (+15.79%)
Mutual labels:  asyncio
nestjs-throttler-storage-redis
Redis storage provider for the nestjs-throttler package.
Stars: ✭ 56 (-41.05%)
Mutual labels:  throttler
yutto
🧊 一个可爱且任性的 B 站视频下载器(bilili V2)
Stars: ✭ 383 (+303.16%)
Mutual labels:  asyncio
python-logi-circle
Python 3.6+ API for Logi Circle cameras
Stars: ✭ 23 (-75.79%)
Mutual labels:  asyncio
triviaroyale
Trivia game in the browser using websockets and asyncio.
Stars: ✭ 13 (-86.32%)
Mutual labels:  asyncio
timvt
PostGIS based Vector Tile server.
Stars: ✭ 113 (+18.95%)
Mutual labels:  asyncio
async retrial
Python package for retrial of asyncio based coroutines
Stars: ✭ 14 (-85.26%)
Mutual labels:  asyncio
bolsa
Biblioteca feita em Python com o objetivo de facilitar o acesso a dados de seus investimentos na bolsa de valores(B3/CEI) através do Portal CEI.
Stars: ✭ 46 (-51.58%)
Mutual labels:  asyncio
discord-hero
A powerful, modular and easy-to-use Discord bot framework. Batteries included!
Stars: ✭ 30 (-68.42%)
Mutual labels:  asyncio
aiosc
Lightweight Open Sound Control implementation for Python using asyncio
Stars: ✭ 26 (-72.63%)
Mutual labels:  asyncio
aiotinydb
asyncio compatibility shim for tinydb
Stars: ✭ 42 (-55.79%)
Mutual labels:  asyncio
duckpy
A simple Python library for searching on DuckDuckGo.
Stars: ✭ 20 (-78.95%)
Mutual labels:  asyncio
thanker
Don't be a wanker, be a thanker! Automatically give thanks to Pypi packages you use in your project.
Stars: ✭ 25 (-73.68%)
Mutual labels:  asyncio
HibiAPI
一个实现了多种常用站点的易用化API的程序 / A program that implements easy-to-use APIs for a variety of commonly used sites.
Stars: ✭ 427 (+349.47%)
Mutual labels:  asyncio
hikari
A Discord API wrapper for Python and asyncio built on good intentions.
Stars: ✭ 631 (+564.21%)
Mutual labels:  asyncio

asyncio-throttle

travis-ci shields-pypi-badge

Simple, easy-to-use throttler for asyncio.

Example

import time
import random
import asyncio

from asyncio_throttle import Throttler

async def worker(no, throttler, n):
    for _ in range(n):
        await asyncio.sleep(random.random() * 2)

        async with throttler:
            print(time.time(), 'Worker #%d: Bang!' % no)

async def main():
    throttler = Throttler(rate_limit=5)

    tasks = [
        loop.create_task(worker(no, throttler, 10))
            for no in range(5)
    ]
    await asyncio.wait(tasks)

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

Here I limited work rate to 5/sec while there are 5 workers. And the result:

1508273760.3462772 Worker #2: Bang!
1508273760.590009 Worker #3: Bang!
1508273760.856431 Worker #0: Bang!
1508273761.0110679 Worker #2: Bang!
1508273761.086856 Worker #4: Bang!
1508273761.350699 Worker #3: Bang!
1508273761.5906 Worker #1: Bang!
1508273761.8655958 Worker #4: Bang!
1508273762.224158 Worker #0: Bang!
1508273762.600234 Worker #2: Bang!
1508273762.694332 Worker #2: Bang!
1508273762.726774 Worker #0: Bang!
1508273762.944273 Worker #4: Bang!

Installation

$ pip install asyncio-throttle

It requires Python 3.6 or later.

Usage

asyncio_throttle.Throttler introduces simple APIs: flush() and acquire(). But you will not be interested in those because you can just use it within with statement and it looks nicer.

First, create a throttler given desired rate limit. For example if you want to limit rate to 500/min, you can make it as:

from asyncio_throttle import Throttler

throttler = Throttler(rate_limit=500, period=60)

Then whenever you want to do some jobs which should have limited rate(e.g. sending request to server), Put it in async with statement:

async with throttler:
    send_a_request()

It's that easy. asyncio_throttler can be easily integrated with aiohttp too:

async def worker(throttler, session):
    while True:
        async with throttler:
            async with session.get('http://example.com') as resp:
                do_some_job_with(await resp.text())

        await asyncio.sleep(0.05)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].