Skip to main content

Setting a limitation for usage frequency. Read more:

Project description

frequency_controller PyPIGitHub Workflow StatusPyPI - WheelPyPI - Python VersionPyPI - DownloadsPyPI - License

Limitations of frequency. Code snippets copied from torequests.


  1. There are many implementations for frequency-control, the generator way is better than using a Queue
    1. Queue instances use more memory
    2. Queue initial process is slower than create a generator
  2. Python3.8+ required the lock protection for async-generators.
    2. But 3.6 / 3.7 don't have this feature
  3. Using timeit.default_timer for a better accuracy but little performance lost.
    1. Frequency.TIMER = timeit.default_timer


pip install -U frequency_controller

Quick Start

1. Multi-Thread Demo

from frequency_controller import Frequency
from threading import Thread
from time import strftime

def sync_demo():
    # limit to 2 concurrent tasks each 1 second
    frequency = Frequency(2, 1)

    def test():
        with frequency:
            print(strftime('%Y-%m-%d %H:%M:%S'))

    threads = [Thread(target=test) for _ in range(5)]
    for t in threads:

if __name__ == "__main__":
    # 2020-02-21 18:35:43
    # 2020-02-21 18:35:43
    # 2020-02-21 18:35:44
    # 2020-02-21 18:35:44
    # 2020-02-21 18:35:45

2. Coroutine Demo

from asyncio import ensure_future, get_event_loop, wait
from time import strftime

from frequency_controller import AsyncFrequency

async def async_demo():
    frequency = AsyncFrequency(2, 1)

    async def task():
        async with frequency:
            print(strftime('%Y-%m-%d %H:%M:%S'))

    tasks = [ensure_future(task()) for _ in range(5)]
    await wait(tasks)

if __name__ == "__main__":
    # python3.7 use
    # 2020-02-21 18:43:51
    # 2020-02-21 18:43:51
    # 2020-02-21 18:43:52
    # 2020-02-21 18:43:52
    # 2020-02-21 18:43:53

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for frequency-controller, version 0.0.6
Filename, size File type Python version Upload date Hashes
Filename, size frequency_controller-0.0.6-py2-none-any.whl (5.2 kB) File type Wheel Python version py2 Upload date Hashes View
Filename, size frequency_controller-0.0.6-py3-none-any.whl (5.2 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page