Форум сайта python.su
У нас есть поток заявок, обслуживаемых 1 устройтвом. Ниже приведено программа для реализации данной системмы с помошью пакета SimPy.
1 2 """A counter with a random service time""" 3 from SimPy.Simulation import * 4 from random import expovariate, seed 5 6 class Source(Process): 7 """ Source generates customers randomly""" 8 9 def generate(self,number,interval): 10 for i in range(number): 11 c = Customer(name = "Customer%02d"%(i,)) 12 activate(c,c.visit(timeInBank=12.0)) 13 t = expovariate(1.0/interval) 14 yield hold,self,t 15 16 class Customer(Process): 17 """ Customer arrives, is served and leaves """ 18 19 def visit(self,timeInBank=0): 20 arrive=now() 21 print "%7.4f %s: Here I am "%(now(),self.name) 22 yield request,self,counter 23 wait=now()-arrive 24 print "%7.4f %s: Waited %6.3f"%(now(),self.name,wait) 25 tib = expovariate(1.0/timeInBank) 26 yield hold,self,tib 27 yield release,self,counter 28 print "%7.4f %s: Finished "%(now(),self.name) 29 30 def model(theseed): 31 global counter 32 seed(theseed) 33 counter = Resource(name="Karen") 34 35 initialize() 36 source = Source(’Source’) 37 activate(source,source.generate(5,interval=10.0),0.0) 38 simulate(until=400.0) 39 40 model(theseed=12345)
Офлайн