Python Forum
How to yield multiple requests but only action one request in Simpy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to yield multiple requests but only action one request in Simpy
#3
(Apr-05-2020, 06:09 PM)keuninkske Wrote: i have no experience with simpy, eather with the type of queue's you are talking about

but have you been testing your case already?
can you give the guys over here at least a part of the source code
and the error you are running into

So with the below code, the users need either resource t1 or t2 in order to finish whatever it is they are doing. The problem I'm having is, whilst they only need t1 or t2, by the nature of the process, the first user requests both t1 and t2, and since it is the first user, they are both available so he/she actually takes both resources. Now the second user puts in a request for t1 and t2 immediately after the first user, but because the first user took both of the resources instead of just the one that he/she needed, the second user (and all the others) are having to wait.


import simpy
import random

env = simpy.Environment() # Simpy requirement

t1 = simpy.Resource(env, capacity = 1) # A resource that can only be used by 1 process at a time
t2 = simpy.Resource(env, capacity = 1) # A resoruce that can only be used by 1 process at a time

def user(env, name, t1, t2):
# The process needs either resource t1 or t2 to do what it needs to do
req1 = t1.request() # Process requests t1
req2 = t2.request() # Process requests t2

yield req1 | req2 # The process waits until either request for t1 OR request for t2 is satisfied

period = random.randint(1, 10)

yield env.timeout(period) # Wait a random amount of time between 1 - 10

if req1.triggered:
t1.release(req1) # If the request for t1 was successful, you can now release the resource
if req2.triggered:
t2.release(req2) # If the request for t2 was successful, you can now release the resource

print("At %d minutes, User %d has finished. They took %d mins to finish." % (env.now, name, period))

for i in range(10):
env.process(user(env, i, t1, t2))

env.run()
Reply


Messages In This Thread
RE: How to yield multiple requests but only action one request in Simpy - by BushChooks - Apr-08-2020, 01:15 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020