Simpy如何异步使用过滤器存储

2024-05-18 11:16:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Simpy来模拟机器修理工作。机器可以进厂修理,指派一名技术员,然后继续修理,直到修理完毕。在

我使用过滤器商店来跟踪技术人员。在

NUM_TECHNICIANS = 3               # number of technicians
REPAIR_TIME = 5              # time to repair machine in hours

def task_network(technicians):

    # Request a technician    
    t = yield technicians.get()
    print("Assigned to Technician %d at time %d" % (t.id, env.now))
    yield env.process(t.repair_machine(machine1))

    t = yield technicians.get(lambda t: t.fatigue < 30)
    print("Assigned to Technician %d at time %d" % (t.id, env.now))
    yield env.process(t.repair_machine(machine2))

class Machine(object):
    def __init__(self, env, id, type, isBroken):
        self.env = env
        self.id = id
        self.type = type
        self.isBroken = isBroken

class Technician(object):
    def __init__(self, env, id, skill_level, fatigue, shifts_remaining):
        self.env = env
        self.id = id
        self.skill_level = skill_level
        self.fatigue = fatigue
        self.shifts_remaining = shifts_remaining
        self.isAvailable = True

    def repair_machine(self, machine):
        if machine.type == "MN152":
            self.fatigue += 10
            self.shifts_remaining -= 0.25
            self.isAvailable = False
            print("Repairing...")
            yield env.timeout(REPAIR_TIME)
            print("Technician %d is done repairing at time %d" % (self.id, env.now))

env = simpy.Environment()

# Filter Store allows us to have processes ask for objects as resources (the technicians)
# and get them based off of some criteria (e.g. this radio is complex so requires a more experienced technician)
# If no criteria is specified, Filter Store is FIFO
technicians = simpy.FilterStore(env, capacity = NUM_TECHNICIANS)

t0 = Technician(env, id=0, skill_level=74, fatigue=15, shifts_remaining=2)
t1 = Technician(env, id=1, skill_level=45, fatigue=50, shifts_remaining=1)
t2 = Technician(env, id=2, skill_level=56, fatigue=0, shifts_remaining=3)

technicians.put(jt0)
technicians.put(jt1)
technicians.put(jt2)

machine1 = Machine(env, id=0, type="MN150", isBroken=True)
machine2 = Machine(env, id=1, type="MN152", isBroken=True)

env.process(task_network(technicians))
env.run()

上面的代码运行正常并打印出来

^{pr2}$

但是,如何使机器能够异步进入、分配、修复和返回呢?当前(由于我的yield语句),模拟将挂起,直到yield进程返回,这就是为什么它要等到一台机器完成修复后再启动另一台机器修复作业。商店里有三个技术员,所以应该允许三个人异步地修理和归还机器。我如何实现这一点?谢谢您。在


Tags: selfenv机器idtypemachinelevelskill