Simpy - 如何异步使用Filter Store

时间:2017-04-10 14:52:41

标签: python simpy

我正在使用Simpy来模拟机器维修工作。机器可以进入工厂进行维修,指派技术人员,然后进行维修,直至完成。

我正在使用Filter Store来跟踪技术人员。

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()

上面的代码按原样运行并打印

Assigned to Technician 0 at time 0
Repairing...
Technician 0 is done repairing at time 5
Assigned to Technician 2 at time 5
Repairing...
Technician 2 is done repairing at time 10

但是,如何使机器可以进入,分配,修复和异步返回?目前(由于我的yield语句),模拟暂停,直到yield进程返回,这就是为什么它等到一台机器完成修复之后才开始另一个机器修复工作。商店里有三名技术人员,所以它应该允许所有三个人异步修理和返回机器。我该如何实现这一目标?谢谢。

1 个答案:

答案 0 :(得分:2)

您可以生成多个进程,然后使用Environment.all_of等待它们:

procs = [env.process(my_task() for _ in range(3))

# do other stuff ...

results = yield env.all_of(procs)