Skip to main content

Thread-safe rate limit semaphore for threading and async/await style built on Rust

Project description

Ralipyard

PyPI - Python Version PyPI - Implementation PyPI

Python thread-safe rate limit semaphore for threading and async/await style built on Rust's library raliguard

Overview

For example, crate a semaphore that allows only 5 calls per 1 second

import asyncio
import time
import datetime

import ralipyard


async def task(sem: ralipyard.Semaphore, no: int):
    delay = sem.calc_delay()
    await asyncio.sleep(delay)
    print(f"Task {no=} executes its code at {time.monotonic():.5f}")


async def main():
    sem = ralipyard.Semaphore(
        access_times=5,
        per_period=datetime.timedelta(seconds=1)
    )
    tasks = [task(sem, i) for i in range(100)]
    await asyncio.gather(*tasks)


asyncio.run(main())

Output:

Task no=0 executes its code at 0.21575
Task no=4 executes its code at 1.22018
Task no=3 executes its code at 1.22022
Task no=2 executes its code at 1.22023
Task no=1 executes its code at 1.22024
Task no=9 executes its code at 2.21618
Task no=7 executes its code at 2.21622
Task no=8 executes its code at 2.21623
Task no=6 executes its code at 2.21624
Task no=5 executes its code at 2.21624
Task no=11 executes its code at 3.22016
Task no=10 executes its code at 3.22020
Task no=12 executes its code at 3.22021
Task no=14 executes its code at 3.22022
Task no=13 executes its code at 3.22023
...

Instalation

From PyPI:

python -m pip install ralipyard

Or from GitHub

python -m pip install https://github.com/deknowny/archive/ralipyard.zip

Documentation

It's unnecessary because the library has the only 1 class with the only 2 methods, visit stubs for more information

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

ralipyard-0.1.0.tar.gz (5.5 kB view hashes)

Uploaded Source

Built Distributions

ralipyard-0.1.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl (985.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ x86-64

ralipyard-0.1.0-cp310-none-win_amd64.whl (121.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

ralipyard-0.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl (983.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ x86-64

ralipyard-0.1.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (439.3 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

ralipyard-0.1.0-cp39-none-win_amd64.whl (121.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

ralipyard-0.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (983.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

ralipyard-0.1.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (439.5 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

ralipyard-0.1.0-cp38-none-win_amd64.whl (121.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

ralipyard-0.1.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (983.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

ralipyard-0.1.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (439.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

ralipyard-0.1.0-cp37-none-win_amd64.whl (121.0 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

ralipyard-0.1.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (983.3 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.5+ x86-64

ralipyard-0.1.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (439.2 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

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