使用打印机的模型是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. 深入理解Java中的IO

    深入理解Java中的IO 引言:     对程序语言的设计者来说,创建一个好的输入/输出(I/O)系统是一项艰难的任务 < Thinking in Java >   本文的目录视图如下: ...

  2. servlet3.1

    Servlet3.1新增的新特性强制更改sessionId 由HttpServletRequest 的changeSessionId()方法实现 非阻塞式IO 非阻塞式IO我们应该知道Servlet底 ...

  3. jstat查看JVM GC情况

    转自 https://www.cnblogs.com/yjd_hycf_space/p/7755633.html

  4. cdn节点自定义防CC代码在哪里抄

    1. 登陆节点的3311后台 1. http redirect(普通) HTTP/1.1 302 FOUNDConnection: keep-aliveLocation: {{url}} <ht ...

  5. MongoDB:索引操作

    首先插入十万个数据 ;i;i++){ var rand = parseInt(i*Math.random()); db.person_test.insert({"name":&qu ...

  6. animation渐进实现点点点等待效果

    <style>    @keyframes dot {     0% { width: 0; }     33% { width: .2em; }     66% { width: .5e ...

  7. netty(六) websocket开发应用

    package com.lance.net.server.common; import java.net.InetSocketAddress; import org.springframework.s ...

  8. 远程连接ORACLE服务

    远程服务端操作系统: Windows Server 2003 Enterprise Edition sp2ORACLE 版本: Oracle 9.2.0.1.0 正式版 本地客户端操作系统: Wind ...

  9. FloatingActionButton FAB 悬浮按钮

    FloatingActionButton简称FAB,这是一种比较美观的按钮: 1.使用前: FAB代表一个App或一个页面中最主要的操作,如果一个App的每个页面都有FAB,则通常表示该App最主要的 ...

  10. 545. Boundary of Binary Tree二叉树的边界

    [抄题]: Given a binary tree, return the values of its boundary in anti-clockwise direction starting fr ...