收率保持到其他线> 0

时间:2014-10-28 00:54:21

标签: python yield simpy

我正在使用SimPy 2.3并且有一个以随机速率在ATM上生成客户的流程以及以随机速率为客户提供服务的另一个流程。当该行为空时,我希望ATM在做其他任何事情之前等待下一个客户。

这是一些代码

class ATM(Process):
    def Run(self):
        while 1:
            if self.atm_line.customers is 0:
                yield hold, wait for self.atm_line.customers != 0 # this is the line I'm stuck on
            yield hold, self, random.expovariate(.1)
            self.atm_line.customers -= 1

class ATM_Line(Process):
    def __init__(self):
        self.customers = 0
        Process.__init__(self)

    def Run(self):
        while 1:
            yield hold, self, random.expovariate(.1)
            self.customers += 1

initialize()
a = ATM()
a.atm_line = ATM_Line()
activate(a, a.Run())
activate(a.atm_line, a.atm_line.Run())
simulate(until=10000)

这样做的好方法是什么?

1 个答案:

答案 0 :(得分:1)

我能够使用yield waitevent和信号来解决这个问题。工作代码如下。

from SimPy.Simulation import *
from random import Random, expovariate, uniform

class ATM(Process):
    def Run(self):
        while 1:
            if self.atm_line.customers is 0:
                yield waitevent, self, self.atm_line.new_customer
            yield hold, self, random.expovariate(.05)
            self.atm_line.customers -= 1

class ATM_Line(Process):
    def __init__(self):
        self.customers = 0
        self.new_customer = SimEvent()
        Process.__init__(self)

    def Run(self):
        while 1:
            yield hold, self, random.expovariate(.07)
            self.new_customer.signal()
            self.customers += 1

initialize()
a = ATM()
a.atm_line = ATM_Line()
activate(a, a.Run())
activate(a.atm_line, a.atm_line.Run())
simulate(until=50)