Simpy get elements that are waiting for a Resource to be free
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 |
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 |
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
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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