python--使用队列结构来模拟共享打印机等候时间

按书里的样例抄的。

可以看到,将打印速度由第分钟5页提高到10页之后,

每个学生提交打印任务到打印完成的时间明显缩短。

===========================

在计算机科学实验室里考虑下面的情况。

平均每天大约10名学生在实验室工作,每人每小时打印2次。

这些打印任务的长度范围从1到20页。

实验室中的打印机较旧,每分钟以草稿质量可以处理10页。

打印机可以切换以提供更好的质量,但是它将每分钟只能处理5页。

较慢的打印速度可能会使学生等待太久。应使用什么页面速率?

===========================

# coding = utf-8
import random


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 is not None:
            self.time_remaining = self.time_remaining - 1
            if self.time_remaining <= 0:
                self.current_task = None

    def busy(self):
        if self.current_task is not None:
            return True
        else:
            return False

    def start_next(self, new_task):
        self.current_task = new_task
        self.time_remaining = new_task.get_pages() * 60/self.page_rate


class Task:
    def __init__(self, time):
        self.time_stamp = time
        self.pages = random.randrange(1, 21)

    def get_stamp(self):
        return self.time_stamp

    def get_pages(self):
        return self.pages

    def wait_time(self, current_time):
        return current_time - self.time_stamp


class Queue:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    # 入队列,加在首部
    def enqueue(self, item):
        self.items.insert(0, item)

    # 出队列,最后一个
    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)


def simulation(num_seconds, pages_per_minutes):
    lab_printer = Printer(pages_per_minutes)
    print_queue = Queue()
    waiting_times = []

    for current_second in range(num_seconds):
        if new_print_task():
            task = Task(current_second)
            print_queue.enqueue(task)
        if (not lab_printer.busy()) and (not print_queue.is_empty()):
            next_task = print_queue.dequeue()
            waiting_times.append(next_task.wait_time(current_second))
            lab_printer.start_next(next_task)
        lab_printer.tick()
    average_wait = sum(waiting_times)/len(waiting_times)
    print('Average Wait %6.2f seconds %3d tasks remaining.'% (average_wait, print_queue.size()))


def new_print_task():
    num = random.randrange(1, 181)
    if num == 180:
        return True
    else:
        return False


print('=============printer 5ppm===============')
for i in range(10):
    simulation(3600, 5)
print('=============printer 10ppm===============')
for i in range(10):
    simulation(3600, 10)

  

C:\Users\Sahara\.virtualenvs\untitled\Scripts\python.exe D:/test/python_queue.py
=============printer 5ppm===============
Average Wait 299.63 seconds   3 tasks remaining.
Average Wait 219.16 seconds   1 tasks remaining.
Average Wait  67.89 seconds   0 tasks remaining.
Average Wait  28.47 seconds   0 tasks remaining.
Average Wait  88.47 seconds   0 tasks remaining.
Average Wait 274.36 seconds   1 tasks remaining.
Average Wait  71.88 seconds   0 tasks remaining.
Average Wait  58.25 seconds   0 tasks remaining.
Average Wait  92.94 seconds   0 tasks remaining.
Average Wait  78.45 seconds   0 tasks remaining.
=============printer 10ppm===============
Average Wait  12.36 seconds   0 tasks remaining.
Average Wait   5.55 seconds   0 tasks remaining.
Average Wait  10.00 seconds   0 tasks remaining.
Average Wait  16.12 seconds   0 tasks remaining.
Average Wait  49.08 seconds   0 tasks remaining.
Average Wait  15.68 seconds   0 tasks remaining.
Average Wait  12.71 seconds   0 tasks remaining.
Average Wait  34.44 seconds   0 tasks remaining.
Average Wait  10.07 seconds   0 tasks remaining.
Average Wait  21.60 seconds   0 tasks remaining.

  python--使用队列结构来模拟共享打印机等候时间

上一篇:Python : 标准库-测试模块


下一篇:python – Numpy平均函数舍入错误