| 1 |
from SimPy.Simulation import * |
| 2 |
from random import uniform,seed |
| 3 |
import string |
| 4 |
|
| 5 |
def theTime(time): |
| 6 |
|
| 7 |
hrs=int(time/60) |
| 8 |
min=int(time-hrs*60) |
| 9 |
return "%s:%s" %(string.zfill(str(hrs),2),string.zfill(str(min),2)) |
| 10 |
|
| 11 |
class worker(Process): |
| 12 |
def __init__(self,id): |
| 13 |
Process.__init__(self) |
| 14 |
self.id=id |
| 15 |
self.output=0 |
| 16 |
self.idle=0 |
| 17 |
self.total_idle=0 |
| 18 |
|
| 19 |
def working_day(self,foobar): |
| 20 |
print "%s Worker %s arrives in factory" %(theTime(now()),self.id) |
| 21 |
while now()<17*60: |
| 22 |
yield hold,self,uniform(3,10) |
| 23 |
|
| 24 |
foobar.queue.append(self) |
| 25 |
if foobar.idle: |
| 26 |
reactivate(foobar) |
| 27 |
else: |
| 28 |
self.idle=1 |
| 29 |
start_idle=now() |
| 30 |
|
| 31 |
yield passivate,self |
| 32 |
self.output += 1 |
| 33 |
if self.idle: |
| 34 |
self.total_idle+=now()-start_idle |
| 35 |
self.idle=0 |
| 36 |
print "%s %s goes home, having built %d widgets today." %(theTime(now()),self.id,self.output) |
| 37 |
print "Worker %s was idle waiting for foobar machine for %3.1f hours" %(self.id,self.total_idle/60) |
| 38 |
|
| 39 |
class foobar_machine(Process): |
| 40 |
def __init__(self): |
| 41 |
Process.__init__(self) |
| 42 |
self.queue=[] |
| 43 |
self.idle=1 |
| 44 |
|
| 45 |
def foobar_Process(self): |
| 46 |
yield passivate,self |
| 47 |
while 1: |
| 48 |
while len(self.queue) > 0: |
| 49 |
self.idle=0 |
| 50 |
yield hold,self,3 |
| 51 |
served=self.queue.pop(0) |
| 52 |
reactivate(served) |
| 53 |
self.idle=1 |
| 54 |
yield passivate,self |
| 55 |
|
| 56 |
seed(111333555) |
| 57 |
print 'SimPy_worker_extend' |
| 58 |
initialize() |
| 59 |
foo=foobar_machine() |
| 60 |
activate(foo,foo.foobar_Process(),delay=0) |
| 61 |
john=worker("John") |
| 62 |
activate(john,john.working_day(foo),at=510) |
| 63 |
eve=worker("Eve") |
| 64 |
activate(eve,eve.working_day(foo),at=510) |
| 65 |
simulate(until=18*60) |
| 66 |
|