coroutine - 示例
分享流畅的python一书, coroutine 章节中的出租车仿真的例子. from collections import namedtuple
import queue
import random
Event = namedtuple('Event', 'time process action') def taxi_simu(ident, trips, start_t = ):
time = yield Event(start_t, ident, 'get out of garage')
for i in range(trips):
time = yield Event(time, ident, 'pick up passenger')
time = yield Event(time, ident, 'passenger arrive destination') yield Event(time, ident, 'off duty, going home') class Emulator(object): def __init__(self, proc_mapping):
self.events = queue.PriorityQueue() # 保存排定事件的 PriorityQueue 对象,按时间正向排序。
self.process = dict(proc_mapping) # 获取的 procs_map 参数是一个字典(或其他映射),可是又从中构建一个字典,创建本地副本,
# 因为在仿真过程中,出租车回家后会从 self.procs 属性中移除,而我们不想修改用户传入的对象
'''
优先队列是离散事件仿真系统的基础构件:创建事件的顺序不定,放入这种队列之后,可以按照各个事件排定的时间顺序取出。
''' def comupte_duration(self, previous_action):
if 'get out of garage' in previous_action:
return random.randint(,)
elif 'pick up passenger' in previous_action:
return random.randint(,)
elif 'passenger arrive destination' in previous_action:
return random.randint(,)
else:
return
def run(self, end_time):
for _, process in sorted(self.process.items()): # 使用 sorted 函数获取 self.procs 中按键排序的元素;用不到键,因此赋值给 _。
first_event = next(process) # 调用 next(proc) 预激各个协程,向前执行到第一个 yield 表达式,做好接收数据的准备。产出一个 Event 对象。
self.events.put(first_event) # 把各个事件添加到 self.events 属性表示的 PriorityQueue 对象中。 simu_time = # 把 sim_time 变量(仿真钟)归零
while simu_time < end_time:
if self.events.empty(): # 如果队列中没有未完成的事件,退出主循环
print('empty events queue, all events done')
break current_event = self.events.get() # 获取优先队列中 time 属性最小的 Event 对象;这是当前事件(current_event)
simu_time, process_id, previous_action = current_event # 拆包 Event 对象中的数据。这一行代码会更新仿真钟 sim_time,对应于事件发生时的时间。
# 这通常是离散事件仿真:每次循环时仿真钟不会以固定的量推进,而是根据各个事件持续的时间推进
print('Taxi : ', process_id, process_id * ' ', current_event) # 显示 Event 对象,指明是哪辆出租车,并根据出租车的编号缩进
actived_process = self.process[process_id] # 从 self.procs 字典中获取表示当前活动的出租车的协程
next_time = simu_time + self.comupte_duration(previous_action)
try:
next_event = actived_process.send(next_time) # 把计算得到的时间发给出租车协程。协程会产出下一个事件(next_event),或者抛出 StopIteration 异常(完成时)
except StopIteration:
del self.process[process_id] # 如果抛出了 StopIteration 异常,从 self.procs 字典中删除那个协程
else:
self.events.put(next_event) # 否则,把 next_event 放入队列中
else:
msg = '=== end of simulation time : {} events are pending ==='
print(msg.format(self.events.qsize())) # 如果循环由于仿真时间到了而退出,显示待完成的事件数量(有时可能碰巧是零, 如 endtime 足够大,就不有 event pending, 都会处理完) if __name__ == '__main__':
taxi_num =
DEPARTURE_INTERVAL =
end_time =
taxis = {i: taxi_simu(i, (i+)*, i*DEPARTURE_INTERVAL)
for i in range(taxi_num)} simu = Emulator(taxis)
simu.run(end_time) '''
OUTPUT, Taxi : Event(time=, process=, action='get out of garage')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='get out of garage')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='get out of garage')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='get out of garage')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='get out of garage')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='off duty, going home')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='off duty, going home')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='off duty, going home')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='off duty, going home')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='pick up passenger')
Taxi : Event(time=, process=, action='passenger arrive destination')
Taxi : Event(time=, process=, action='off duty, going home')
empty events queue, all events done '''
coroutine - 示例的更多相关文章
- Coroutine in Java - Quasar Fiber实现--转载
转自 https://segmentfault.com/a/1190000006079389?from=groupmessage&isappinstalled=0 简介 说到协程(Corout ...
- 【Unity3D基础教程】给初学者看的Unity教程(五):详解Unity3D中的协程(Coroutine)
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 为什么需要协程 在游戏中有许多过程(Proc ...
- Lua 协程coroutine
协程和一般多线程的区别是,一般多线程由系统决定该哪个线程执行,是抢占式的,而协程是由每个线程自己决定自己什么时候不执行,并把执行权主动交给下一个线程. 协程是用户空间线程,操作系统其存在一无所知,所以 ...
- Unity3D协同程序(Coroutine)
摘要下: 1. coroutine, 中文翻译"协程".这个概念可能有点冷门,不过百度之,说是一种很古老的编程模型了,以前的操作系统里进程调度里用到过,现在操作系统的进程调度都是根 ...
- unity Dotween插件的简单介绍及示例代码
unity里面做插值动画的插件有许多,比较常见的有itween.hotween.dotween.根据大家的反馈和实际体验来说,dotween插件在灵活性.稳定性.易用性上都十分突出.这里简单介绍下它的 ...
- (zt)Lua的多任务机制——协程(coroutine)
原帖:http://blog.csdn.net/soloist/article/details/329381 并发是现实世界的本质特征,而聪明的计算机科学家用来模拟并发的技术手段便是多任务机制.大致上 ...
- 协程coroutine
协程(coroutine)顾名思义就是“协作的例程”(co-operative routines).跟具有操作系统概念的线程不一样,协程是在用户空间利用程序语言的语法语义就能实现逻辑上类似多任务的编程 ...
- Coroutine,你究竟干了什么?
一 引子 使用Unity已经有一段时间了,对于Component.GameObject之类的概念也算是有所了解,而脚本方面从一开始就选定了C#,目前来看还是挺明智的:Boo太小众,而且支持有限:JS( ...
- Lua 5.3 协程简单示例
Lua 5.3 协程简单示例 来源 http://blog.csdn.net/vermilliontear/article/details/50547852 生产者->过滤器->消费者 模 ...
随机推荐
- Spring--2.Spring之IOC--IOC容器的23个实验(1)
实验1.IOC容器创建对象,并为属性赋值 Hello World:(通过各种方式给容器中注册对象(注册会员)) 以前是自己new对象,现在所有对象交给容器创建:给容器中注册组件 以后框架编写流程: ...
- js删除数组的某个元素
最近在刷算法题,我是用js语言去写的,其中一题需要删除数组的某个元素,查了一下资料,总结一下 使用splice()方法 array.splice(start[, deleteCount[, item1 ...
- python基础操作以及变量运用
今天学习关于pycharm的操作以及变量的知识 1.关于pycharm的基本操作,作为一个小白,仪式感还是要有 在基础界面上新建然后打印hello world,也是对python的一种尊重吧 2.关于 ...
- Vue中echarts的基本用法
前言:同大多数的前端框架一样,先读官网的使用方法.学会基本使用后,在实例中找到自己想要demo.拿过来改一改,一个echarts图表就形成,毕竟人家做就是为了方便使用. 我是在vue中下面直接使用的e ...
- 【Java并发基础】Java内存模型解决有序性和可见性
前言 解决并发编程中的可见性和有序性问题最直接的方法就是禁用CPU缓存和编译器的优化.但是,禁用这两者又会影响程序性能.于是我们要做的是按需禁用CPU缓存和编译器的优化. 如何按需禁用CPU缓存和编译 ...
- VMware Workstation CentOS7 Linux 学习之路(4)--守护服务(Supervisor)
目前存在三个问题 问题1:ASP.NET Core应用程序运行在shell之中,如果关闭shell则会发现ASP.NET Core应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生 ...
- unittest模块使用方法
unittest模块常用属性 1. unittest.TestCase类:所有的测试用例类继承的基类 定义一个测试用例类,需要继承TestCase,比如: class BaiduTest(unitte ...
- Django admin的简单使用
1.汉化网站 位置:setting.py LANGUAGE_CODE = 'zh-hans' 2.注册站点(添加表) 位置:admin.py from app01 import models admi ...
- 百度搜索关键词联想API JSONP使用实例
许多搜索引擎都提供了关键词联想api,且大多数都是jsonp. Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获 ...
- 分层有限状态机的C++实现
为了方便我的游戏开发,写了这么一个通用的分层有限状态机.希望在其稳定以后,可以作为一个组件加入到我的游戏引擎当中. 目前使用了std::function来调用回调函数,在未来可能会用委托机制代替. 第 ...