cristiklein / simpy

MIT License
107 stars 16 forks source link

Resource not getting released #7

Open GGurtner opened 4 years ago

GGurtner commented 4 years ago

Hi guys, I have been using simpy for a while now but I have an issue with shared resources that I can't figure tout. Since it is part of a complex simulations, I do not have a simple reproducible example for now.

In short, I have resources used by different users. I create the requests for the users, everything is fine. When I release a resource, everything works fine most of the time. However, it happens that some requests never get succeeded, even though the previous is released. So:

In fact, is the last point possible at all in theory in simpy? Shouldn't it be always the case that an event in the users list is triggered?

NB: resources have capacity 1.

GGurtner commented 4 years ago

Another thing I do not understand, which might be linked to this. The following code:

import simpy 

def resource_user(env, resource, idd=None):
    request = resource.request()  # Generate a request event
    request.id = idd
    yield request                 # Wait for access
    yield env.timeout(20)          # Do something
    print ('Before release:', idd, resource.users, resource.queue, env.now)
    resource.release(request)     # Release the resource
    print ('After release:', idd, resource.users, resource.queue, env.now)

class Test:
    def __init__(self, env):
        self.env = env
        res = simpy.Resource(env, capacity=1)
        self.env.process(resource_user(env, res, idd=1))
        self.env.process(resource_user(env, res, idd=2))

env = simpy.Environment()
test = Test(env)
env.run()

Has the following output:

Before release: 1 [<Request() object at 0x7fabca899550>] [<Request() object at 0x7fabca899668>] 20
After release: 1 [] [<Request() object at 0x7fabca899668>] 20
Before release: 2 [<Request() object at 0x7fabca899668>] [] 40
After release: 2 [] [] 40

I do not understand why the users and queue lists are different in the second and third lines. More specifically, I do not understand why in the second line, after the resource has been released, the users is still emtpy, as if the release has not been performed completely already. Is something happening at the end of the time step?

cristiklein commented 4 years ago

Hi,

Can you try to things?

(1) IIRC, you are not supposed to override the id method, since some frameworks might use that to establish object equality.

(2) You are really not supposed to touch the request before yield. Can you move the assignment after yield?

My 2 cents, Cristian

On Mon, 21 Oct 2019, 21:29 Gérald Gurtner, notifications@github.com wrote:

Another thing I do not understand, which might be linked to this. The following code:

import simpy def resource_user(env, resource, idd=None): request = resource.request() # Generate a request event request.id = idd yield request # Wait for access yield env.timeout(20) # Do something print ('Before release:', idd, resource.users, resource.queue, env.now) resource.release(request) # Release the resource print ('After release:', idd, resource.users, resource.queue, env.now) class Test: def init(self, env): self.env = env res = simpy.Resource(env, capacity=1) self.env.process(resource_user(env, res, idd=1)) self.env.process(resource_user(env, res, idd=2))

env = simpy.Environment() test = Test(env) env.run()

Has the following output:

Before release: 1 [<Request() object at 0x7fabca899550>] [<Request() object at 0x7fabca899668>] 20 After release: 1 [] [<Request() object at 0x7fabca899668>] 20 Before release: 2 [<Request() object at 0x7fabca899668>] [] 40 After release: 2 [] [] 40

I do not understand why the users and queue lists are different in the second and third lines. More specifically, I do not understand why in the second line, after the resource has been released, the users is still emtpy, as if the release has not been performed completely already. Is something happening at the end of the time step?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/cristiklein/simpy/issues/7?email_source=notifications&email_token=AAMVPINHFK33AMTVODKMCOLQPXYHLA5CNFSM4JDCKBL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEB3KH5I#issuecomment-544646133, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMVPIOA2ZOGGYRJWLWMB23QPXYHLANCNFSM4JDCKBLQ .