All Projects โ†’ uburuntu โ†’ Throttler

uburuntu / Throttler

Licence: mit
๐Ÿ”€โณ Easy throttling with asyncio support

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Throttler

Python Parallel Programming Cookbook Cn
๐Ÿ“–ใ€ŠPython Parallel Programming Cookbookใ€‹ไธญๆ–‡็‰ˆ
Stars: โœญ 978 (+1530%)
Mutual labels:  asyncio
Whalesong
Whalesong is an asyncio python library to manage WebApps remotely. Currently WhatsappWeb is implemented
Stars: โœญ 46 (-23.33%)
Mutual labels:  asyncio
Pytile
๐Ÿ“ก A simple Python API for Tileยฎ Bluetooth trackers
Stars: โœญ 56 (-6.67%)
Mutual labels:  asyncio
Exchange Rates
๐Ÿ’ฑ Querying a rate-limited currency exchange API using Redis as a cache
Stars: โœญ 37 (-38.33%)
Mutual labels:  rate-limiter
Uvloop
Ultra fast asyncio event loop.
Stars: โœญ 8,246 (+13643.33%)
Mutual labels:  asyncio
Webfsd
A simple HTTP server for mostly static content written in C
Stars: โœญ 50 (-16.67%)
Mutual labels:  asyncio
Python Webarchive
Create WebKit/Safari .webarchive files on any platform
Stars: โœญ 30 (-50%)
Mutual labels:  asyncio
Web Applications With Fastapi Course
Demo code and other handouts for students of our FastAPI Web Apps course.
Stars: โœญ 56 (-6.67%)
Mutual labels:  asyncio
Bucket4j
Java rate limiting library based on token/leaky-bucket algorithm.
Stars: โœญ 1,025 (+1608.33%)
Mutual labels:  rate-limiter
Aiomultiprocess
Take a modern Python codebase to the next level of performance.
Stars: โœญ 1,070 (+1683.33%)
Mutual labels:  asyncio
Rocketgram
Modern and powerful asynchronous telegram bot framework.
Stars: โœญ 37 (-38.33%)
Mutual labels:  asyncio
Async
An awesome asynchronous event-driven reactor for Ruby.
Stars: โœญ 1,000 (+1566.67%)
Mutual labels:  asyncio
Aiodine
๐Ÿงช Async-first Python dependency injection library
Stars: โœญ 51 (-15%)
Mutual labels:  asyncio
Aioworkers
Easy configurable workers based on asyncio
Stars: โœญ 35 (-41.67%)
Mutual labels:  asyncio
Beanie
Micro ODM for MongoDB
Stars: โœญ 56 (-6.67%)
Mutual labels:  asyncio
Kopf
A Python framework to write Kubernetes operators in just few lines of code.
Stars: โœญ 971 (+1518.33%)
Mutual labels:  asyncio
Halive
A fast http and https prober, to check which URLs are alive
Stars: โœญ 47 (-21.67%)
Mutual labels:  asyncio
Aiopg
aiopg is a library for accessing a PostgreSQL database from the asyncio
Stars: โœญ 1,097 (+1728.33%)
Mutual labels:  asyncio
Vibe Core
Repository for the next generation of vibe.d's core package.
Stars: โœญ 56 (-6.67%)
Mutual labels:  asyncio
Socketshark
A WebSocket message router based on Python/Redis/asyncio
Stars: โœญ 51 (-15%)
Mutual labels:  asyncio

Throttler

Python PyPI License: MIT

Build Status codecov Codacy Badge

Zero-dependency Python package for easy throttling with asyncio support.

Demo

๐Ÿ“ Table of Contents

๐ŸŽ’ Install

Just

pip install throttler

๐Ÿ›  Usage Examples

All run-ready examples are here.

Throttler and ThrottlerSimultaneous

Throttler:

Context manager for limiting rate of accessing to context block.

from throttler import Throttler

# Limit to three calls per second
t = Throttler(rate_limit=3, period=1.0)
async with t:
    pass

Or

import asyncio

from throttler import throttle

# Limit to three calls per second
@throttle(rate_limit=3, period=1.0)
async def task():
    return await asyncio.sleep(0.1)

ThrottlerSimultaneous:

Context manager for limiting simultaneous count of accessing to context block.

from throttler import ThrottlerSimultaneous

# Limit to five simultaneous calls
t = ThrottlerSimultaneous(count=5)
async with t:
    pass

Or

import asyncio

from throttler import throttle_simultaneous

# Limit to five simultaneous calls
@throttle_simultaneous(count=5)
async def task():
    return await asyncio.sleep(0.1)

Simple Example

import asyncio
import time

from throttler import throttle


# Limit to two calls per second
@throttle(rate_limit=2, period=1.0)
async def task():
    return await asyncio.sleep(0.1)


async def many_tasks(count: int):
    coros = [task() for _ in range(count)]
    for coro in asyncio.as_completed(coros):
        _ = await coro
        print(f'Timestamp: {time.time()}')

asyncio.run(many_tasks(10))

Result output:

Timestamp: 1585183394.8141203
Timestamp: 1585183394.8141203
Timestamp: 1585183395.830335
Timestamp: 1585183395.830335
Timestamp: 1585183396.8460555
Timestamp: 1585183396.8460555
...

API Example

import asyncio
import time

import aiohttp

from throttler import Throttler, ThrottlerSimultaneous


class SomeAPI:
    api_url = 'https://example.com'

    def __init__(self, throttler):
        self.throttler = throttler

    async def request(self, session: aiohttp.ClientSession):
        async with self.throttler:
            async with session.get(self.api_url) as resp:
                return resp

    async def many_requests(self, count: int):
        async with aiohttp.ClientSession() as session:
            coros = [self.request(session) for _ in range(count)]
            for coro in asyncio.as_completed(coros):
                response = await coro
                print(f'{int(time.time())} | Result: {response.status}')


async def run():
    # Throttler can be of any type
    t = ThrottlerSimultaneous(count=5)        # Five simultaneous requests
    t = Throttler(rate_limit=10, period=3.0)  # Ten requests in three seconds

    api = SomeAPI(t)
    await api.many_requests(100)

asyncio.run(run())

Result output:

1585182908 | Result: 200
1585182908 | Result: 200
1585182908 | Result: 200
1585182909 | Result: 200
1585182909 | Result: 200
1585182909 | Result: 200
1585182910 | Result: 200
1585182910 | Result: 200
1585182910 | Result: 200
...

ExecutionTimer

Context manager for time limiting of accessing to context block. Simply sleep period secs before next accessing, not analog of Throttler. Also it can align to start of minutes.

import time

from throttler import ExecutionTimer

et = ExecutionTimer(60, align_sleep=True)

while True:
    with et:
        print(time.asctime(), '|', time.time())

Or

import time

from throttler import execution_timer

@execution_timer(60, align_sleep=True)
def f():
    print(time.asctime(), '|', time.time())

while True:
    f()

Result output:

Thu Mar 26 00:56:17 2020 | 1585173377.1203406
Thu Mar 26 00:57:00 2020 | 1585173420.0006166
Thu Mar 26 00:58:00 2020 | 1585173480.002517
Thu Mar 26 00:59:00 2020 | 1585173540.001494

Timer

Context manager for pretty printing start, end, elapsed and average times.

import random
import time

from throttler import Timer

timer = Timer('My Timer', verbose=True)

for _ in range(3):
    with timer:
        time.sleep(random.random())

Or

import random
import time

from throttler import timer

@timer('My Timer', verbose=True)
def f():
    time.sleep(random.random())

for _ in range(3):
    f()

Result output:

#1 | My Timer | begin: 2020-03-26 01:46:07.648661
#1 | My Timer |   end: 2020-03-26 01:46:08.382135, elapsed: 0.73 sec, average: 0.73 sec
#2 | My Timer | begin: 2020-03-26 01:46:08.382135
#2 | My Timer |   end: 2020-03-26 01:46:08.599919, elapsed: 0.22 sec, average: 0.48 sec
#3 | My Timer | begin: 2020-03-26 01:46:08.599919
#3 | My Timer |   end: 2020-03-26 01:46:09.083370, elapsed: 0.48 sec, average: 0.48 sec

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Author

Ramzan Bekbulatov:

๐Ÿ’ฌ Contributing

Contributions, issues and feature requests are welcome!

๐Ÿ“ License

This project is MIT licensed.

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].