1

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!

Ziva
  • 3,181
  • 15
  • 48
  • 80
  • You're probably going to have to use [threads](https://docs.python.org/3/library/threading.html) to run those processes simultaneously. – Hylke Nov 16 '20 at 15:49
  • @Hylke Oh, I see, so simpy on its own cannot handle it? An example of how to do it given my code would be very appriciated! – Ziva Nov 16 '20 at 16:05
  • 1
    I'm not really familiar with the library, so I'm afraid I cannot give you an example. But I'm fairly certain you do need that library, or something similar, to run multiple python processes simultaneously. That's because by design Python is a procedural language, meaning it just executes everything step-by-step – Hylke Nov 16 '20 at 16:52
  • Coud you not just call "queue_checking" within the function "def add_packets(self)" each time a new package was added to the queue? – StefanOverFlow Oct 22 '22 at 10:52
  • were you able to solve this issue? – veritaS Jul 09 '23 at 17:58

0 Answers0