Using simpy
I want to implement a simple queueing
simulator, in which packets are processed in batches of a given size. In order words: I want to have a simulator in which packets are sent (the packets are send at some random intervals). When the packet arrives, it is added to a queue. Now, I would like to have a process (independent of the packets sending process) that processes the packets arriving into a queue in batches of size 10
. So, once there are at least 10 packets in the queue, the process_queue
function is triggered to process them (i.e., print on the screen or do something else). Once this is finished, the function checks again whether there are at least 10
packets in the queue. If yes, it takes the first 10
packets and processes. If there are less than 10
packets in the queue, the function waits for more packets to arrive. Thus, the batches are processed one after the other, and not in parallel.
So far this is what I implemented:
import simpy
import random
class Queue:
def __init__(self, env):
self.env = env
self.queue = []
env.process(self.add_packets())
def add_packets(self):
i = 0
while i < 1000:
self.queue.append(i)
i += 1
self.env.timeout(random.randint(0, 20))
def queue_checking(self):
if len(self.queue) > 10:
self.env.process(self.process_queue())
def process_queue(self):
batch = self.queue[:10]
for i in batch:
self.queue.remove(i)
// here is some operations I do on the packet
print("Finished processing batch")
The issue which I have is that I don't know how to now combine those 3 functions, in such a way that when there are at least 10 packets in the queue the process_queue
function is triggered, but at the same time, once the function process_queue
is triggered we have to wait for it to finish before we start to process another 10 packets from the queue, i.e., before we call the same function again (if we should). So, ultimately I would like my simulator to keep receiving packets yet at the same time process, the received packets as: take the first 10 packets from the queue, process, check and if applicable (i.e., if there are more than 10 packets) go take next 10, process, go check and get next 10, etc etc. Many thanks in advance!