设置使用频率限制。阅读更多:https://github.com/ClericPy/frequency_控制器。
frequency-controller的Python项目详细描述
频率控制器
频率限制。从torequests复制的代码段。在
简介
- 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
安装
pip install -U frequency_controller
快速入门
1。多线程演示
fromfrequency_controllerimportFrequencyfromthreadingimportThreadfromtimeimportstrftimedefsync_demo():# limit to 2 concurrent tasks each 1 secondfrequency=Frequency(2,1)deftest():withfrequency:print(strftime('%Y-%m-%d %H:%M:%S'))threads=[Thread(target=test)for_inrange(5)]fortinthreads: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。协同程序演示
^{pr2}$- 项目
标签: