Simpy get elements that are waiting for a Resource to be free
up vote
1
down vote
favorite
I'm working with a simple simulation with Simpy and Python.
My goal is to have a resource that can be 1 at the time, and count all the other processes that are waiting for that resource to be free.
Example :
Person 1 comes, takes the resource. waiting is 0
Person 2 arrives, waits. waiting is 1
Person 3 arrives, waits. waiting is 2
Person 1 leaves, releasing resource, so now Person 2 takes it. waiting is 1
This is my code so far:
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
Calling the Simulation
env = simpy.Environment()
env.process(env1(env))
env.run(until=500)
I've tried using .get_queue method , but it's always empty.
Using .queue seems to always add elements, but never removes them from the queue.
I've also tried using the put and release methods, but nothing seems to work.
I do not understand correctly how this methods work, and how to achieve this.
Any ideas?
Thanks!
python simpy
add a comment |
up vote
1
down vote
favorite
I'm working with a simple simulation with Simpy and Python.
My goal is to have a resource that can be 1 at the time, and count all the other processes that are waiting for that resource to be free.
Example :
Person 1 comes, takes the resource. waiting is 0
Person 2 arrives, waits. waiting is 1
Person 3 arrives, waits. waiting is 2
Person 1 leaves, releasing resource, so now Person 2 takes it. waiting is 1
This is my code so far:
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
Calling the Simulation
env = simpy.Environment()
env.process(env1(env))
env.run(until=500)
I've tried using .get_queue method , but it's always empty.
Using .queue seems to always add elements, but never removes them from the queue.
I've also tried using the put and release methods, but nothing seems to work.
I do not understand correctly how this methods work, and how to achieve this.
Any ideas?
Thanks!
python simpy
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm working with a simple simulation with Simpy and Python.
My goal is to have a resource that can be 1 at the time, and count all the other processes that are waiting for that resource to be free.
Example :
Person 1 comes, takes the resource. waiting is 0
Person 2 arrives, waits. waiting is 1
Person 3 arrives, waits. waiting is 2
Person 1 leaves, releasing resource, so now Person 2 takes it. waiting is 1
This is my code so far:
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
Calling the Simulation
env = simpy.Environment()
env.process(env1(env))
env.run(until=500)
I've tried using .get_queue method , but it's always empty.
Using .queue seems to always add elements, but never removes them from the queue.
I've also tried using the put and release methods, but nothing seems to work.
I do not understand correctly how this methods work, and how to achieve this.
Any ideas?
Thanks!
python simpy
I'm working with a simple simulation with Simpy and Python.
My goal is to have a resource that can be 1 at the time, and count all the other processes that are waiting for that resource to be free.
Example :
Person 1 comes, takes the resource. waiting is 0
Person 2 arrives, waits. waiting is 1
Person 3 arrives, waits. waiting is 2
Person 1 leaves, releasing resource, so now Person 2 takes it. waiting is 1
This is my code so far:
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
Calling the Simulation
env = simpy.Environment()
env.process(env1(env))
env.run(until=500)
I've tried using .get_queue method , but it's always empty.
Using .queue seems to always add elements, but never removes them from the queue.
I've also tried using the put and release methods, but nothing seems to work.
I do not understand correctly how this methods work, and how to achieve this.
Any ideas?
Thanks!
python simpy
python simpy
asked Nov 11 at 1:46
Nicolas Gallegos
121312
121312
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
After some research and trial/error, I've found the solution.
Basically, when you use "with res.request() as" statement, you let the put/release interaction to be done by itself, which helps to avoid errors.
In order to get the queue status, or interact with it, you only have to call it AFTER the with statement: (since the element will be inside the resource queue AFTER the with statement)
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
print("QUEUE SIZE: ",len(res.queue))
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
After some research and trial/error, I've found the solution.
Basically, when you use "with res.request() as" statement, you let the put/release interaction to be done by itself, which helps to avoid errors.
In order to get the queue status, or interact with it, you only have to call it AFTER the with statement: (since the element will be inside the resource queue AFTER the with statement)
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
print("QUEUE SIZE: ",len(res.queue))
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
add a comment |
up vote
0
down vote
accepted
After some research and trial/error, I've found the solution.
Basically, when you use "with res.request() as" statement, you let the put/release interaction to be done by itself, which helps to avoid errors.
In order to get the queue status, or interact with it, you only have to call it AFTER the with statement: (since the element will be inside the resource queue AFTER the with statement)
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
print("QUEUE SIZE: ",len(res.queue))
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
After some research and trial/error, I've found the solution.
Basically, when you use "with res.request() as" statement, you let the put/release interaction to be done by itself, which helps to avoid errors.
In order to get the queue status, or interact with it, you only have to call it AFTER the with statement: (since the element will be inside the resource queue AFTER the with statement)
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
print("QUEUE SIZE: ",len(res.queue))
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
After some research and trial/error, I've found the solution.
Basically, when you use "with res.request() as" statement, you let the put/release interaction to be done by itself, which helps to avoid errors.
In order to get the queue status, or interact with it, you only have to call it AFTER the with statement: (since the element will be inside the resource queue AFTER the with statement)
import simpy
def env1(env):
res = simpy.Resource(env,capacity=1)
while True:
yield env.timeout(5)
print("Arriving Person at ",(env.now))
env.process(getResource(env, res))
def getResource(env,res):
with res.request() as req:
print("QUEUE SIZE: ",len(res.queue))
yield req
print("Person using resource at ", env.now)
yield env.timeout(20)
print("Leaving at ", env.now)
answered Nov 12 at 2:42
Nicolas Gallegos
121312
121312
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245148%2fsimpy-get-elements-that-are-waiting-for-a-resource-to-be-free%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown