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
#1
Within Simpy, I have multiple resources that can do the same job, but they are different so I can't just increase the capacity. Picture a single queue in a shopping centre that leads to all tellers. Some are manned and some are self serve. I put a request for both (two separate requests), and then yield rq_manned OR rq_selfserve, satisfied if atleast one of the requests is granted.

The problem is, what if they both become available at the same time, I don't want to actually request them both. What to do?
Reply
#2
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
Reply
#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
#4
I actually just found a way to solve this myself. It would be pretty hard without exploring the in's and out's of Simpy. For those interested in the future, just stick these two lines after the yield statement.

if req1 in requests and req2 in requests:
t2.release(req2)
Reply


Forum Jump:

User Panel Messages

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