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
Metadata
Release files for frequency-controller 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| frequency_controller-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
| frequency_controller-0.0.6-py2-none-any.whl | Python 2 | none | any | Details |
Total release size: 10.5 kB
Release files / frequency_controller-0.0.6-py3-none-any.whl
| Download URL | frequency_controller-0.0.6-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3d3cb17e3ec19a7d3a3d408f5fee5ddf8aefd099fadde32ba15d8879c221da4a
|
|
BLAKE2b-256 checksum How to use checksums |
ef03b2972f52abd8448bbcab6f73856d9abe2863643542f054a842d6eb2891d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / frequency_controller-0.0.6-py2-none-any.whl
| Download URL | frequency_controller-0.0.6-py2-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
f9017ba485ffc6fa4c9dfa21dbcbe1cf2290ab5f60949e3d1d3823e08b6a40eb
|
|
BLAKE2b-256 checksum How to use checksums |
9e41dcad93b615cb244481a8a174ee059536d34231f25330f046c61c015c76e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|