Priority Queues in SimPy

515 views
Skip to first unread message

Shesha Sreenivasamurthy

unread,
Mar 12, 2016, 2:08:04 AM3/12/16
to python-simpy
I have a producer-consumer set up, where producer is generating objects and consumer is consuming them. I have create a Store resource: x = simpy.Store(self.env, capacity=simpy.core.Infinity)

on producer I perform:
-----------------------------
obj = Object()
x.put(obj)

on consumer I perform: 
--------------------------------
with x.get() as req:
    yield req
obj = req.value
<process object here>

This works perfectly fine. However, I want to now add priority to the objects so if the producer produces a priority object, the consumer should get that object ahead of all the other objects producer has produced earlier. How to go about achieving this ?

For example:
---------------------
Producer produces objects: 'a', 'b', 'c', 'Z', 'd'  at time 0,1, 2, 3 & 4. 'Z' has higher priority than 'a', 'b', 'c' and 'd'.

Say, consumer needs 2 seconds to process each object. At time 0 and 2 consumer process objects 'a' and 'b'. By the time consumer is ready to process its 3rd object (at time 4), producer has produced high priority object 'Z'. Therefore, consumer should process 'Z' before 'c'. How to achieve this ?

Stefan Scherfke

unread,
Mar 14, 2016, 8:25:10 AM3/14/16
to Shesha Sreenivasamurthy, python-simpy
Hi Shesha,

> Am 2016-03-12 um 08:08 schrieb Shesha Sreenivasamurthy <she...@gmail.com>:
>
> I have a producer-consumer set up, where producer is generating objects and consumer is consuming them. I have create a Store resource: x = simpy.Store(self.env, capacity=simpy.core.Infinity)
>
> on producer I perform:
> -----------------------------
> obj = Object()
> x.put(obj)
>
> on consumer I perform:
> --------------------------------
> with x.get() as req:
> yield req
> obj = req.value
> <process object here>

"obj = yield req" would be more idiomatic in this case. Most cases (e.g, if
you don't cancel request), you can even just do "obj = yield store.get()".

> This works perfectly fine. However, I want to now add priority to the objects so if the producer produces a priority object, the consumer should get that object ahead of all the other objects producer has produced earlier. How to go about achieving this ?
>
> For example:
> ---------------------
> Producer produces objects: 'a', 'b', 'c', 'Z', 'd' at time 0,1, 2, 3 & 4. 'Z' has higher priority than 'a', 'b', 'c' and 'd'.

You could implement a new subclass of Store, where you override "_do_get()".
Instead of returning the first element as "Store" does, you could return the
element with the highest priority currently in the store. You can also use
the FilterStore as a source of inspiration :)

Cheers,
Stefan

>
> Say, consumer needs 2 seconds to process each object. At time 0 and 2 consumer process objects 'a' and 'b'. By the time consumer is ready to process its 3rd object (at time 4), producer has produced high priority object 'Z'. Therefore, consumer should process 'Z' before 'c'. How to achieve this ?
>
> --
> You received this message because you are subscribed to the Google Groups "python-simpy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python-simpy...@googlegroups.com.
> To post to this group, send email to python...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/python-simpy/57b52cd9-ba6c-42ce-95cb-1ab7703dfc6e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages