Giter Club home page Giter Club logo

async-throttle's Introduction

Async Throttle

Multipurpose concurrency primitive for asyncio coroutines.

Features

This throttle is configured with two related, but different parameters:

Throttle(capacity: float, concurrency: int = 0, period: float = 1.0)

capacity - Sets the rate limit for requests, as capacity per period seconds.

concurrency - The number of jobs that can be executing at a given time.

Usually, servers will set policies on both of these dimensions, and will suspend clients that violate either of them.

Throttle#pause(td: int)

The pause method will lock the throttle for the given number of seconds.

For example, if an API bans your client from accessing resources due to violating their rate-limit, you can tell your code to sleep for a period of time and try again later.

Usage

The throttle can be a drop-in replacement for another primitive like asyncio.Semaphore. In fact, it's really just an asyncio.Semaphore (which handles the concurrency limit) mixed with a token bucket to provide rate limiting.

throttle = Throttle(10, 2)  # Two concurrent coros limited to 10qps (total).

# Perform one task when the budget allows
async with throttle:
    # Do some (async) thing

# For tasks that should consume more of the budget, you can call the throttle:
async with throttle(5):
    # Do some more expensive thing, equivalent to 5 requests

Like other asyncio primitives, Throttle is not thread-safe.

async-throttle's People

Contributors

jnu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.