Creating custom simpy resources

363 views
Skip to first unread message

Camilla Montonen

unread,
May 11, 2016, 3:42:52 PM5/11/16
to python-simpy
Hi Simpy users, 
I would like to create a custom shared resource that will notify all processes yielding a put request, if they have been put into the queue (something like the below)

resource = simpy.Resource(env, capacity=1)
def user(env, resource):
    req = resource.request()
    result = yield req | resource.resource_capacity_full
    if req in result:
        print 'Place received'
    else:
        print 'Waiting for resource'

in this case resource_capacity_full would be an Event that is triggered as successful once the Resource is at capacity. 
Is there a simple way to do this? perhaps make a subclass of resource and then add a method?

Would really appreciate to hear your ideas/comments.

Kind regards,
Camilla

jpgr...@gmail.com

unread,
May 11, 2016, 5:03:10 PM5/11/16
to python-simpy
For the particular scenario identified in your code, I think the existing Resource has what you need. You can test resource.count against resource.capacity to determine whether requesting the resource would block (wait).

resource = simpy.Resource(env, capacity=1)
def user(env, resource):

   
if resource.count < resource.capacity:
       
with resource.request() as req:
            t0
= env.now
           
yield req
           
assert t0 == env.now
           
print 'acquired'
   
else:
       
print 'would wait'

Are there other scenarios you are thinking about that this technique would not serve?

I've had situations where I have wanted an event when a Resource becomes available or when a Store becomes non-empty, but without also acquiring the Resource or popping an item from the Store. Those scenarios would require a non-standard event.

Cheers,
Pete

Stefan Scherfke

unread,
May 12, 2016, 1:57:28 AM5/12/16
to jpgr...@gmail.com, python-simpy
Hi Camilla and Pete,

you can also use this guide (http://simpy.readthedocs.io/en/latest/topical_guides/monitoring.html) for inspiration :)

Cheers,
Stefan
> --
> 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/5e45e302-06be-45f2-8ee6-5033dd2874e3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages