这是我试图在 SimPy 中实现的:
客户首先进入服务器 A 服务 5 分钟。然后每个客户分成两个,一个去Server B服务3分钟,一个去Server C服务2分钟。
我编写了以下代码,但根据输出似乎无法正常工作。另外我不确定我使用复制功能的方式是否正确,因为我想拆分每个离开服务器 A 的客户,但我觉得我的代码可能只适用于第一个客户。
任何帮助,将不胜感激。
from SimPy.Simulation import *
from copy import *
class Source(Process):
def generate(self):
for i in range(5):
c = Customer(name="Customer%02d" % (i))
activate(c, c.runA())
yield hold, self, 10
class Customer(Process):
def runA(self):
print("%7.4f %s (original) enters server A" % (now(), self.name))
yield hold, self, 5
print("%7.4f %s (original) leaves server A" % (now(), self.name))
dup1 = copy(self)
dup2 = copy(self)
activate(dup1, dup1.runB())
activate(dup2, dup2.runC())
def runB(self):
print("%7.4f %s (copy) enters server B" % (now(), self.name))
yield hold, self, 3
print("%7.4f %s (copy) leaves server B" % (now(), self.name))
def runC(self):
print("%7.4f %s (copy) enters server C" % (now(), self.name))
yield hold, self, 2
print("%7.4f %s (copy) leaves server C" % (now(), self.name))
initialize()
s = Source()
activate(s, s.generate(), at=0.0)
simulate(until=100.0)
输出:
0.0000 Customer00 (original) enters server A
5.0000 Customer00 (original) leaves server A
10.0000 Customer01 (original) enters server A
15.0000 Customer01 (original) leaves server A
20.0000 Customer02 (original) enters server A
25.0000 Customer02 (original) leaves server A
30.0000 Customer03 (original) enters server A
35.0000 Customer03 (original) leaves server A
40.0000 Customer04 (original) enters server A
45.0000 Customer04 (original) leaves server A
logout