Search code examples
pythonsimulationsimpy

Simpy how to access objects in a resource queue


I am moving code written in Simpy 2 to version 3 and could not find an equivalent to the following operation.

In the code below I access job objects (derived from class job_(Process)) in a Simpy resource's activeQ.

def select_LPT(self, mc_no):
    job = 0
    ptime = 0
    for j in buffer[mc_no].activeQ:
        if j.proc_time[mc_no] > ptime:
            ptime = j.proc_time[mc_no]
            job = j

    return job

To do this in Simpy 3, I tried the following

buffers[mc_no].users

which returns a list of Request() objects. With these objects I cannot access the processes that created them, nor the objects these process functions belong to. (using the 'put_queue', and 'get_queue' of the Resource object did not help)

Any suggestions ?


Solution

  • In SimPy, request objects do not know which process created them. However, since we are in Python land you can easily add this information:

    with resource.request() as req:
        req.obj = self
        yield req
        ...
    
     # In another process/function
     for user_req in resource.users:
         print(user_req.obj)