Professional Documents
Culture Documents
21BCE2670
21BCE2670
Slot: L25+L26
Date: 20-07-2023
Name: KAIBADI.DILEEP
RegNo: 21BCE2670
Question:
import time
class LeakyBucket:
def __init__(self, bucket_size, output_rate):
self.bucket_size = bucket_size
self.output_rate = output_rate
self.bucket_content = 0
self.last_time = time.time()
def get_tokens(self):
now = time.time()
elapsed_time = now - self.last_time
tokens_to_add = elapsed_time *
self.output_rate
self.bucket_content = max(0,
self.bucket_content - tokens_to_add)
self.last_time = now
Output:
1. Class Definition:
Create a class called LeakyBucket for the
Leaky Bucket Algorithm implementation.
Initialize the class with two parameters:
2. get_tokens Method:
Get the current time.
update.
Calculate the number of tokens to remove
time.
3. accept_packet Method:
Call the get_tokens method to update the