문제

I am having trouble with resource priority in simpy. Consider the following code:

import simpy

env = simpy.Environment()
res = simpy.PriorityResource(env, capacity = 1)

def go(id):
    with res.request(priority = id) as req:
        yield req
        print id,res

env.process(go(3))
env.process(go(2))
env.process(go(4))
env.process(go(5))
env.process(go(1))
env.run()

Lower number means higher priority, so I should get 1,2,3,4,5. But instead i am getting 3,1,2,4,5. So the first output is wrong, after that its sorted!

Thanks in advance for your help.

도움이 되었습니까?

해결책

This is correct. When "3" requests the resource, it is empty so it gets the slot. The remaining processes have to queue and will get the resource in the order 1, 2, 4, 5.

If you use the PreemptiveResource instead (like request(priority=id, preempt=True)), 3 will still get the resource first but will be preempted by 2. 2 will then get preempted by 1. 2 and 3 would then have to request the resource again to gain access to it.

다른 팁

Even I had the same problem where I was supposed to make a factory FIFO. At that time I assigned a reaction time to a part and made it to follow the previous part. That is only if the previous part had got into service of resource, I made the next part request. It solved the problem objectively but seemed like it slowed down the simulation little and also gave a rexn time to the part. It was basically a revamp of the factory process. But I would love to see a feature when the part doesn't have to request again. Can it be done in the present version?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top