This document is for Kombu's development version, which can be significantly different from previous releases. Get the stable docs here: 5.0.

速率限制- kombu.utils.limits

用于速率限制的令牌桶实现。

class kombu.utils.limits.TokenBucket(fill_rate, capacity=1)[源代码]

令牌桶算法。

参见

https

//en.wikipedia.org/wiki/Token_Bucket大部分代码是从ASPN Python Cookbook中的条目中窃取的:

https

//code.activestate.com/recipes/511490/

警告:

线程安全:此实现不是线程安全的。访问 TokenBucket 实例应该出现在任何多线程代码的临界区内。

add(item)[源代码]
can_consume(tokens=1)[源代码]

检查是否可以消费一个或多个令牌。

返回:

bool --从水桶里。如果它们可以被消费,则调用也将从存储桶中消费所请求数量的令牌。呼叫将仅消耗 tokens (请求的数量)或零个令牌--它永远不会消耗部分数量的令牌。

返回类型:

true if the number of tokens can be consumed

capacity = 1

存储桶中的最大令牌数。

clear_pending()[源代码]
expected_time(tokens=1)[源代码]

返回令牌可用的预计时间。

返回:

float

返回类型:

the time in seconds.

fill_rate = None

将重新填充桶的速率(以令牌/秒为单位)。

pop()[源代码]
timestamp = None

上次从桶中取出令牌的时间戳。