Reputation: 121
In a simulation I would like to able to request a resource in one function before calling another function in which the resource is released. When I use the method shown below it doesn’t work and I get an error. Thanks in advance.
(in function 1):
req = resource.request()
yield req
yield.env.process(function2( ))
(in function 2):
resource.release(req)
Is this possible? Let me know what else if any other information is required.
Upvotes: 1
Views: 913
Reputation: 3232
From you’re example it’s not clear why it would not work. The simulation below works as expected:
import simpy
def func1(name, env, res):
req = res.request()
yield req
print(name, 'got resource at', env.now)
yield env.process(func2(name, env, res, req))
print(name, 'done')
def func2(name, env, res, req):
yield env.timeout(1)
yield res.release(req)
print(name, 'released at', env.now)
env = simpy.Environment()
res = simpy.Resource(env, capacity=1)
env.process(func1('A', env, res))
env.process(func1('B', env, res))
env.run()
The output:
A got resource at 0
A released at 1
A done
B got resource at 1
B released at 2
B done
Upvotes: 2