• Stars
    star
    118
  • Rank 299,923 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple, easy-to-use throttler for 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)

More Repositories

1

pykakao

simple kakaotalk loco/http protocol wrapper for python
Python
165
star
2

go-windows-programming

Go Windows Programming Tutorial
Go
55
star
3

syso

πŸ”§ tool for embedding various type of resources in go Windows executable
Go
42
star
4

tmux-reset

Easy way to reset all options to default in tmux
Python
40
star
5

hwp5-table-extractor

A tool for extracting tables from Hwp file.
Python
31
star
6

go-tray-icons-tutorial

Go
25
star
7

ole-py

Lightweight Microsoft OLE file parser in pure Python
Python
17
star
8

python-naverlogin

A tiny module for Naver login.
Python
17
star
9

pyneis

python http client for Neis service(http://neis.go.kr/)
Python
6
star
10

gersang-spritelib

python library for extract or zip Gersang's sprite files
Python
5
star
11

cfb

Microsoft CFB(Compound File Binary) parser
Go
4
star
12

gmaily

Pythonic Gmail client
Python
3
star
13

ikakao

Kakao i Open Builder SDK
Python
3
star
14

gosang

Gersang sprite library for Go
Go
2
star
15

kakaobot

Super simple framework for Kakaotalk auto-reply bot based on aiohttp
Python
2
star
16

hallazzang.github.io

Hanjun Kim's Dev Blog
HTML
1
star
17

labsafe-bypass

Go
1
star
18

easybot

Go
1
star
19

go-snippets

Minimal Go code snippets.
Go
1
star
20

thecamp

TheCamp(http://www.thecamp.or.kr/) HTTP client & tools
Go
1
star
21

react-event-calendar

Calendar with events demo using React
JavaScript
1
star
22

tendermint-toolkit

HTML
1
star
23

ooconsole

Object-oriented console framework written in C++
C++
1
star
24

winapi

Call Windows API from Go
Go
1
star
25

libhwp

Imported repository from https://gitlab.com/sebuls/libhwp
C
1
star
26

aria-go

Go implementation of the ARIA encryption algorithm
Go
1
star
27

ft-drawing

Fourier transform drawing playground
JavaScript
1
star
28

go-worker

Resizable worker goroutine pool
Go
1
star