使用打印机的模型是queue中最经典的应用之一,这里就回顾一下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 start_next(self,new_task):
self.current_task = new_task
self.time_remaining = new_task.get_pages() * 60 /self.page_rate

  打印任务的代码实现:

import random

class Task:
def __init__(self,time):
self.timestamp = time
self.pages = random.randrange(1,21) def get_stamp(self):
return self.timestamp def get_pages(self):
return self.pages def wait_time(self,current_time):
return current_time - self.timestamp

  任务处理:

import random
from queue import *
from Printer import *
from Task import * print random.randrange(0, 101) def simulation(num_seconds,pages_per_minute): lab_printer = Printer(pages_per_minute)
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 secs %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 for i in range(10):
simulation(3600,5)

  测试结果:

Average Wait 1874.00 secs 3572 tasks remaining.
Average Wait 1793.00 secs 3568 tasks remaining.
Average Wait 1700.00 secs 3572 tasks remaining.
Average Wait 1554.00 secs 3573 tasks remaining.
Average Wait 1831.00 secs 3570 tasks remaining.
Average Wait 1723.00 secs 3569 tasks remaining.
Average Wait 1745.00 secs 3568 tasks remaining.
Average Wait 1697.00 secs 3572 tasks remaining.
Average Wait 1596.00 secs 3569 tasks remaining.
Average Wait 1729.00 secs 3572 tasks remaining.

python中基于queue的打印机仿真算法的更多相关文章

  1. python中基于descriptor的一些概念

    python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2.2.1 静态方法 2.2.2 类方法 2.3 新式类(n ...

  2. python中基于descriptor的一些概念(上)

    @python中基于descriptor的一些概念(上) python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2 ...

  3. python中基于descriptor的一些概念(下)

    @python中基于descriptor的一些概念(下) 3. Descriptor介绍 3.1 Descriptor代码示例 3.2 定义 3.3 Descriptor Protocol(协议) 3 ...

  4. python中的Queue

    一.先说说Queue(队列对象) Queue是python中的标准库,可以直接import 引用,之前学习的时候有听过著名的“先吃先拉”与“后吃先吐”,其实就是这里说的队列,队列的构造的时候可以定义它 ...

  5. python中的Queue(队列)详解

    一.Queue简介 python中的队列分类可分为两种: 1.线程Queue,也就是普通的Queue 2.进程Queue,在多线程与多进程会介绍. Queue的种类: FIFO:  Queue.Que ...

  6. python 中的queue 与多进程--待继续

    一.先说说Queue(队列对象) Queue是python中的标准库,可以直接import 引用,之前学习的时候有听过著名的“先吃先拉”与“后吃先吐”,其实就是这里说的队列,队列的构造的时候可以定义它 ...

  7. python中的Queue模块

    queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...

  8. (数据科学学习手札136)Python中基于joblib实现极简并行计算加速

    本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 我们在日常使用Python进行各种数据计算 ...

  9. python 中的queue, deque

    python3 deque(双向队列) 创建双向队列 import collections d = collections.deque() append(往右边添加一个元素) import colle ...

随机推荐

  1. P45 实践作业

    1. 影评: 观众数量多少,决定被虐者死亡速度的快慢.这一新奇但是残忍的想法,无疑是<网络杀机>的点睛之笔.公众.媒体对凶手网站主造成的伤害,比起那些用恶毒言论还要让人难受千百倍.他是一个 ...

  2. Spring使用fastjson处理json数据

    1.搭建SpringMVC+spring环境 2.配置web.xml以及springmvc-config.xml,web.xml同Spring使用jackson处理json数据一样,Springmvc ...

  3. java文件转发

    实际开发情景中,有时会遇到文件需要从一台服务器发到另一台服务器的情况,比如外网发到内网,服务器之间文件同步的情况. 就可以用文件转发. 转发端代码: /** * * @param fileName 保 ...

  4. 365. Water and Jug Problem量杯灌水问题

    [抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...

  5. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  6. Py西游攻关之RabbitMQ、Memcache、Redis

    Py西游攻关之RabbitMQ.Memcache.Redis   RabbitMQ 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议 ...

  7. React-router4 第七篇 Recursive Paths 递归路径

    https://reacttraining.com/react-router/web/example/recursive-paths import React from 'react' import ...

  8. Python开发——数据类型【字符串】

    字符串定义 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息 在Python中加了引号的字符,都被认为是字符串! 单引号.双引号.多引号之间的区别? 答案:单双引号没有区别 多引号的作用? ...

  9. 代码之髓读后感——名字&作用域&类型

    名字和作用域 为什么要取名 看着代码中遍地都是的变量,函数,或多或少的我们都应该想过,为什么会有这些名字呢? 我们知道,计算机将数据存储到对应的物理内存中去.我们的操作就是基于数据的.我们需要使用这些 ...

  10. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...