Queue Example
In this example, we introduce another Py-DES component called Queue. Queues are an essential
part of almost every system in real life.
pydes.Queue(sim, capacity=inf)
Queues are used to acumulate objects in a buffer and retrieved them from it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
Simulator
|
The simulator instance. |
required |
capacity
|
float | int
|
The maximun lenght of the queue. |
inf
|
Methods:
| Name | Description |
|---|---|
put |
tries to insert a new member into the queue and waits if the queue is full. |
get |
tries to get one member from the queue and waits if the queue is empty. |
Constructor for Queue class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
Simulator
|
The simulator instance. |
required |
get()
Get an item from the queue.
Waits until there is an item available in the queue.
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The item retrieved from the queue. |
put(member)
Put an item into the queue or waits if the queue is full.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
member
|
Any
|
The item to be put into the queue. |
required |
size()
Get the size of the queue. Its equivalent to the number of member inside.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of items in the queue. |
Imports
First of all we import the necessary objects from pydes
Define Model
For this example we define a first process that is going to put elements into
the Queue with a time interval.
There is another process running that will try get elements from the same Queue
and wait if it is empty.
Run Simulation
With all this defined. We build the Simulator and the components involved. We schedule
both processes and then start the simulation.
sim = Simulator()
queue = Queue(sim)
p1 = Process1(sim, queue)
p2 = Process2(sim, queue)
sim.schedule(p1.main)
sim.schedule(p2.main)
sim.run(until=30)
The simulation output would resemble the following
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| time | component | value | description |
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| | | | |
| 0 | Process1.0 | Element(id=0) created | None |
| 0 | Process2.0 | requesting an element from queue | None |
| 6 | Process1.0 | inserted element into queue | None |
| 6 | Process1.0 | 1 elements in queue | None |
| 6 | Process1.0 | Element(id=1) created | None |
| 6 | Process2.0 | got Element(id=0) from queue | None |
| 6 | Process2.0 | waiting before getting next element | None |
| 13 | Process1.0 | inserted element into queue | None |
| 13 | Process1.0 | 1 elements in queue | None |
| 13 | Process1.0 | Element(id=2) created | None |
| 19 | Process1.0 | inserted element into queue | None |
| 19 | Process1.0 | 2 elements in queue | None |
| 19 | Process1.0 | Element(id=3) created | None |
| 26 | Process2.0 | requesting an element from queue | None |
| 26 | Process2.0 | got Element(id=1) from queue | None |
| 26 | Process2.0 | waiting before getting next element | None |
| 28 | Process1.0 | inserted element into queue | None |
| 28 | Process1.0 | 2 elements in queue | None |
| 28 | Process1.0 | Element(id=4) created | None |
| 38 | Process1.0 | inserted element into queue | None |
| 38 | Process1.0 | 3 elements in queue | None |
| 38 | Process1.0 | Element(id=5) created | None |