-
Notifications
You must be signed in to change notification settings - Fork 0
/
printerQueue.py
88 lines (53 loc) · 1.52 KB
/
printerQueue.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import random
from queue import Queue
class Printer:
def __init__(self, ppm):
self.page_rate = ppm
self.current_task = None
self.time_remaining = 0
def tick(self):
if self.current_task != None:
self.time_remaining = self.time_remaining - 1
if self.time_remaining <= 0:
self.current_task = None
def busy(self):
if self.current_task != None:
return True
else:
return False
def startNext(self, new_task):
self.current_task = new_task
self.time_remaining = new_task.getPages() * 60 / self.page_rate
class Task:
def __init__(self, time):
self.timestamp = time
self.pages = random.randrange(1, 21)
def getStamp(self):
return self.timestamp
def getPages(self):
return self.pages
def waitTime(self, currentTime):
return currentTime - self.timestamp
def simulation(numSeconds, pagePerMinute):
labPrinter = Printer(pagePerMinute)
printQueue = Queue()
waitingTimes = []
for currentSecond in range (numSeconds):
if newPrintTask():
task = Task(currentSecond)
printQueue.enqueue(task)
if (not labPrinter.busy()) and (not printQueue.is_empty()):
nextTask = printQueue.dequeue()
waitingTimes.append(nextTask.waitTime(currentSecond))
labPrinter.startNext(nextTask)
labPrinter.tick()
averageWait = sum(waitingTimes)/len(waitingTimes)
print("Average wait %6.2f secs %d taks remaining."%(averageWait, printQueue.size()))
def newPrintTask():
num = random.randrange(1, 181)
if num == 180:
return True
else:
return False
for i in range(10):
simulation(3600, 5)