Skip to main content

Setting a limitation for usage frequency. Read more: https://github.com/ClericPy/frequency_controller.

Project description

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

Limitations of frequency. Code snippets copied from torequests.

Intro

  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.
    1. https://bugs.python.org/issue38559
    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

Install

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:
        t.start()


if __name__ == "__main__":
    sync_demo()
    # 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 asyncio.run
    get_event_loop().run_until_complete(async_demo())
    # 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.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

frequency_controller-0.0.6-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

frequency_controller-0.0.6-py2-none-any.whl (5.2 kB view details)

Uploaded Python 2

File details

Details for the file frequency_controller-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: frequency_controller-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for frequency_controller-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3d3cb17e3ec19a7d3a3d408f5fee5ddf8aefd099fadde32ba15d8879c221da4a
MD5 f9e6ce997c9d12b9bb87ecda006127b9
BLAKE2b-256 ef03b2972f52abd8448bbcab6f73856d9abe2863643542f054a842d6eb2891d5

See more details on using hashes here.

File details

Details for the file frequency_controller-0.0.6-py2-none-any.whl.

File metadata

  • Download URL: frequency_controller-0.0.6-py2-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for frequency_controller-0.0.6-py2-none-any.whl
Algorithm Hash digest
SHA256 f9017ba485ffc6fa4c9dfa21dbcbe1cf2290ab5f60949e3d1d3823e08b6a40eb
MD5 cbb3755b08fdace7d91821e76159dda5
BLAKE2b-256 9e41dcad93b615cb244481a8a174ee059536d34231f25330f046c61c015c76e2

See more details on using hashes here.

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