python中基于queue的打印机仿真算法
使用打印机的模型是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的打印机仿真算法的更多相关文章
- python中基于descriptor的一些概念
python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2.2.1 静态方法 2.2.2 类方法 2.3 新式类(n ...
- python中基于descriptor的一些概念(上)
@python中基于descriptor的一些概念(上) python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2 ...
- python中基于descriptor的一些概念(下)
@python中基于descriptor的一些概念(下) 3. Descriptor介绍 3.1 Descriptor代码示例 3.2 定义 3.3 Descriptor Protocol(协议) 3 ...
- python中的Queue
一.先说说Queue(队列对象) Queue是python中的标准库,可以直接import 引用,之前学习的时候有听过著名的“先吃先拉”与“后吃先吐”,其实就是这里说的队列,队列的构造的时候可以定义它 ...
- python中的Queue(队列)详解
一.Queue简介 python中的队列分类可分为两种: 1.线程Queue,也就是普通的Queue 2.进程Queue,在多线程与多进程会介绍. Queue的种类: FIFO: Queue.Que ...
- python 中的queue 与多进程--待继续
一.先说说Queue(队列对象) Queue是python中的标准库,可以直接import 引用,之前学习的时候有听过著名的“先吃先拉”与“后吃先吐”,其实就是这里说的队列,队列的构造的时候可以定义它 ...
- python中的Queue模块
queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...
- (数据科学学习手札136)Python中基于joblib实现极简并行计算加速
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 我们在日常使用Python进行各种数据计算 ...
- python 中的queue, deque
python3 deque(双向队列) 创建双向队列 import collections d = collections.deque() append(往右边添加一个元素) import colle ...
随机推荐
- git配置正确且权限已开但是pull或push提示无权限
因为之前提示输入用户名和密码时输入错误,之后就一直权限认证失败.这种情况下在git bash中输入: git config --system --unset credential.helper 就会重 ...
- 转载:轻量级Config文件AppSettings节点编辑帮助类
using System.Configuration; using System.Windows.Forms; namespace Allyn.Common { public class XmlHep ...
- FortiGate设置E-mail告警
1.配置邮件服务器 2.配置告警
- 信号基础知识---单频矩形脉冲信号CW
%CW%参考:声呐技术 P27,31clc;close all;clear all;%参数-------------------------f0=50;T=0.1;%时宽B=1/T;fs=1000;% ...
- java基础 ----- 选择结构
--------- 流程控制 ------ 流程图 ------ 基本的 if 选择结构 import java.util.Scanner; public class GetPr ...
- ME5xN(x:1-2): custom column is editable in Subscreen Item but not in ALV grid
FM MEMFS_BUILD_MAPPING_PO_VIEWS->LMEMFSF0Z enhancement 14 zenhance_alvg_rid_editable. "activ ...
- ORACLE数据库测试数据插入速度[z]
一,没有优化的速度:Executed in 69.436 seconds drop table t purge;create table t(x int);/*清空共享池,注意在生产环境中千万不能做这 ...
- 【bug小记】应用跳转白屏
tv端项目 测试那边反馈我们的应用跳转到别的应用,再跳转回来会出现白屏的情况. 其实这个原因很简单,就是系统内存不足了把我们的app进程销毁了 所以再回到我们的应用的时候需要重新绘制,而这个" ...
- React-router4 第七篇 Recursive Paths 递归路径
https://reacttraining.com/react-router/web/example/recursive-paths import React from 'react' import ...
- Quartz.NET快速入门指南
最近,在工作中遇到了 Quartz.net 这个组件,为了更好的理解项目代码的来龙去脉,于是决定好好的研究一下这个东西.确实是好东西,既然是好东西,我就拿出来分享一下.万丈高楼平地起,我们也从入门开始 ...