【发布时间】:2019-11-21 11:04:00
【问题描述】:
我正在尝试创建一个装饰器,它将全局验证请求存储桶是否已满。 桶大小为 40,泄漏率为每秒 2 个。 我希望能够在保持 bucker sub 40 的同时处理不同请求的多个实例。 self.bucket_current 怎么可能变为负数?另外,如何调试这些线程以及它们的作用?
from time import sleep
from concurrent.futures import ThreadPoolExecutor
from math import ceil
class Counter:
def __init__(self):
self.bucket_size = 40
self.bucket_current = 0
self.bucket_leak_rate = 2
def countdown(self, secs):
self.bucket_current += secs
print(self.bucket_current)
r = ceil(self.bucket_current/self.bucket_leak_rate)
sleep(1)
while self.bucket_current > 0:
for _ in range(1, r+1):
if self.bucket_current != 1:
self.bucket_current -= 2
else:
self.bucket_current -= 1
print(self.bucket_current)
sleep(1)
c = Counter()
with ThreadPoolExecutor() as executor:
executor.submit(c.countdown, 9)
sleep(3)
executor.submit(c.countdown, 7)
输出:
9
7
5
3
10
8
6
4
2
0
-2
-4
-6
-8
-10
-12
-14
【问题讨论】:
-
你的预期输出是什么?
标签: python multithreading synchronization python-decorators class-variables