超级天然蟒蛇限制器

python-throttle的Python项目详细描述


python versionBuild StatuscodecovPyPI version

python redis支持的限制器

滑木或固定车窗限制器

此模块主要提供两个限制器

  • 固定窗口限制器
    简单地使用redis incr,它的速度大约是滑动版本的10倍,但限制并不平滑,可能会在两个间隔之间的间隙附近溢出阈值大小
  • 滑动窗口限制器
    使用redis有序集,速度慢,但提供更平滑的限制和更可扩展性

安装

pip install python-throttle

虚拟示例用法:

提醒:使用名称空间以避免同一个键上可能发生的冲突

importtimefromlimiterimportFixedWindowLimiterTEST_REDIS_CONFIG={'host':'localhost','port':6379,'db':10}ip="who are you?"throttle=FixedWindowLimiter(threshold=2,interval=3,redis_config=TEST_REDIS_CONFIG,name_space="default")print("first time, blocked?: {}".format(throttle.exceeded(ip)))print("second time, blocked?: {}".format(throttle.exceeded(ip)))print("now I block you, blocked?: {}".format(throttle.exceeded(ip)))time.sleep(3)print("refill energy, blocked?: {}".format(throttle.exceeded(ip)))

输出:

first time blocked?: False
second time blocked?: False
now I block you blocked?: True
refill energy blocked?: False

假海滩标志

它来自我的unittest,不准确,

rate_counter_pressure_test: SlidingRedisCounter
rate_counter_pressure_test: SlidingRedisCounter time count: 1.0075314449495636
rate_counter_pressure_test: FixedWindowRedisCounter
rate_counter_pressure_test: FixedWindowRedisCounter time count: 0.13711917598266155

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
ArrayList Java中的搜索字符串   另一个web应用程序的java访问会话   另一个应用程序中的活动和服务之间的java通信   java根据Json字符串类型将Json字符串转换为对象   eclipse如何解决java中的错误异常。lang.NoSuchMethodError:'java。字符串javax。摆动JOptionPane。showInputDialog(java.lang.String)'   线程“main”java中的安卓异常。lang.NoClassDefFoundError:org/codehaus/jackson/JsonParseException   java如何在安卓 emulator上显示Mat图像?使用NDK   Java在本地读取测试源文件,但在服务器上读取失败   java dowhile循环用于计算输入数字中的数字。故障排除代码   JAva初学者在编写获取成本的方法时遇到困难   java是shell游戏。我如何让物体移动,特别是在特定的曲线上,但顺序是随机的?   java如何区分两个同名的JButton   java为什么我在Spring Boot中需要一个接口?   java将文件路径插入数据库将删除\   使用InterfaceType初始化java对象   java如何部署一个分为Angular、Spring Boot和MySQL的项目?   java如何使用Symja解决不等式?