File: needResources.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (46 lines) | stat: -rw-r--r-- 1,334 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from SimPy.Simulation import *
import random

"""
needResources.py

Demo of waitUntil capability.

Scenario:
Three workers require sets of tools to do their jobs. Tools are shared, scarce
resources for which they compete.
"""

class Worker(Process):
    def work(self,heNeeds=[]):
        def workerNeeds():
            for item in heNeeds:
                if item.n==0:
                    return False
            return True
                     
        while now()<8*60:
            yield waituntil,self,workerNeeds
            for item in heNeeds:
                yield request,self,item
            print "%s %s has %s and starts job" %(now(),self.name,
                [x.name for x in heNeeds])
            yield hold,self,random.uniform(10,30)
            for item in heNeeds:
                yield release,self,item
            yield hold,self,2 #rest
    
random.seed(111333555)
print 'needResources'
initialize()
brush=Resource(capacity=1,name="brush")
ladder=Resource(capacity=2,name="ladder")
hammer=Resource(capacity=1,name="hammer")
saw=Resource(capacity=1,name="saw")
painter=Worker("painter")
activate(painter,painter.work([brush,ladder]))
roofer=Worker("roofer")
activate(roofer,roofer.work([hammer,ladder,ladder]))
treeguy=Worker("treeguy")
activate(treeguy,treeguy.work([saw,ladder]))
print simulate(until=9*60)