How to implement a multithread token bucket in Python?
up vote
0
down vote
favorite
I'd like to implement a multi-threaded token bucket. The producer will produce tokens for the container whereas the consumer will try to take the tokens from the container. If the number of tokens in the container is equal or greater than the tokens that the consumer asks for, then it will return True and take the tokens; otherwise, the request will be rejected and return False.
I'm trying to use the Semaphore to implement it but found it is a bit weird to access the number of tokens in the container using self.container._Semaphore__value. The reason I want to know the value of the semaphore is I want to make sure the consumer can get enough tokens from the bucket; otherwise, I'll reject it.
Maybe I have some misunderstanding of the whole thing. Can anyone point out a better way to implement a multi-threaded token bucket?
from threading import BoundedSemaphore, Thread, Lock
class TokenBucket(object):
def __init__(self, max_capacity, rate):
self.container = BoundedSemaphore(max_capacity)
self.rate = rate
self.lock = Lock()
def consume(self, n):
self.lock.acquire()
if n <= self.container._Semaphore__value:
for _ in range(n):
self.container.acquire()
return True
else:
return False
self.lock.release()
def produce(self):
self.lock.acquire()
for i in range(self.fill_rate):
try:
self.container.release()
except ValueError:
print("Full, skipping.")
self.lock.release()
python multithreading token bucket
add a comment |
up vote
0
down vote
favorite
I'd like to implement a multi-threaded token bucket. The producer will produce tokens for the container whereas the consumer will try to take the tokens from the container. If the number of tokens in the container is equal or greater than the tokens that the consumer asks for, then it will return True and take the tokens; otherwise, the request will be rejected and return False.
I'm trying to use the Semaphore to implement it but found it is a bit weird to access the number of tokens in the container using self.container._Semaphore__value. The reason I want to know the value of the semaphore is I want to make sure the consumer can get enough tokens from the bucket; otherwise, I'll reject it.
Maybe I have some misunderstanding of the whole thing. Can anyone point out a better way to implement a multi-threaded token bucket?
from threading import BoundedSemaphore, Thread, Lock
class TokenBucket(object):
def __init__(self, max_capacity, rate):
self.container = BoundedSemaphore(max_capacity)
self.rate = rate
self.lock = Lock()
def consume(self, n):
self.lock.acquire()
if n <= self.container._Semaphore__value:
for _ in range(n):
self.container.acquire()
return True
else:
return False
self.lock.release()
def produce(self):
self.lock.acquire()
for i in range(self.fill_rate):
try:
self.container.release()
except ValueError:
print("Full, skipping.")
self.lock.release()
python multithreading token bucket
Didn't my answer work for you?
– Darkonaut
Nov 14 at 17:31
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'd like to implement a multi-threaded token bucket. The producer will produce tokens for the container whereas the consumer will try to take the tokens from the container. If the number of tokens in the container is equal or greater than the tokens that the consumer asks for, then it will return True and take the tokens; otherwise, the request will be rejected and return False.
I'm trying to use the Semaphore to implement it but found it is a bit weird to access the number of tokens in the container using self.container._Semaphore__value. The reason I want to know the value of the semaphore is I want to make sure the consumer can get enough tokens from the bucket; otherwise, I'll reject it.
Maybe I have some misunderstanding of the whole thing. Can anyone point out a better way to implement a multi-threaded token bucket?
from threading import BoundedSemaphore, Thread, Lock
class TokenBucket(object):
def __init__(self, max_capacity, rate):
self.container = BoundedSemaphore(max_capacity)
self.rate = rate
self.lock = Lock()
def consume(self, n):
self.lock.acquire()
if n <= self.container._Semaphore__value:
for _ in range(n):
self.container.acquire()
return True
else:
return False
self.lock.release()
def produce(self):
self.lock.acquire()
for i in range(self.fill_rate):
try:
self.container.release()
except ValueError:
print("Full, skipping.")
self.lock.release()
python multithreading token bucket
I'd like to implement a multi-threaded token bucket. The producer will produce tokens for the container whereas the consumer will try to take the tokens from the container. If the number of tokens in the container is equal or greater than the tokens that the consumer asks for, then it will return True and take the tokens; otherwise, the request will be rejected and return False.
I'm trying to use the Semaphore to implement it but found it is a bit weird to access the number of tokens in the container using self.container._Semaphore__value. The reason I want to know the value of the semaphore is I want to make sure the consumer can get enough tokens from the bucket; otherwise, I'll reject it.
Maybe I have some misunderstanding of the whole thing. Can anyone point out a better way to implement a multi-threaded token bucket?
from threading import BoundedSemaphore, Thread, Lock
class TokenBucket(object):
def __init__(self, max_capacity, rate):
self.container = BoundedSemaphore(max_capacity)
self.rate = rate
self.lock = Lock()
def consume(self, n):
self.lock.acquire()
if n <= self.container._Semaphore__value:
for _ in range(n):
self.container.acquire()
return True
else:
return False
self.lock.release()
def produce(self):
self.lock.acquire()
for i in range(self.fill_rate):
try:
self.container.release()
except ValueError:
print("Full, skipping.")
self.lock.release()
python multithreading token bucket
python multithreading token bucket
asked Nov 10 at 1:33
向星雨
1
1
Didn't my answer work for you?
– Darkonaut
Nov 14 at 17:31
add a comment |
Didn't my answer work for you?
– Darkonaut
Nov 14 at 17:31
Didn't my answer work for you?
– Darkonaut
Nov 14 at 17:31
Didn't my answer work for you?
– Darkonaut
Nov 14 at 17:31
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235273%2fhow-to-implement-a-multithread-token-bucket-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Didn't my answer work for you?
– Darkonaut
Nov 14 at 17:31