Setting a limitation for usage frequency. Read more: https://github.com/ClericPy/frequency_controller.
Project description
frequency_controller
Limitations of frequency. Code snippets copied from torequests.
Intro
- There are many implementations for frequency-control, the generator way is better than using a Queue
- Queue instances use more memory
- Queue initial process is slower than create a generator
- Python3.8+ required the lock protection for async-generators.
- https://bugs.python.org/issue38559
- But 3.6 / 3.7 don't have this feature
- Using timeit.default_timer for a better accuracy but little performance lost.
- 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
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d3cb17e3ec19a7d3a3d408f5fee5ddf8aefd099fadde32ba15d8879c221da4a |
|
MD5 | f9e6ce997c9d12b9bb87ecda006127b9 |
|
BLAKE2b-256 | ef03b2972f52abd8448bbcab6f73856d9abe2863643542f054a842d6eb2891d5 |
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9017ba485ffc6fa4c9dfa21dbcbe1cf2290ab5f60949e3d1d3823e08b6a40eb |
|
MD5 | cbb3755b08fdace7d91821e76159dda5 |
|
BLAKE2b-256 | 9e41dcad93b615cb244481a8a174ee059536d34231f25330f046c61c015c76e2 |