使用Asyncio的Coroutine来实现一个有限状态机
如图:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import asyncio
import datetime
import time
from random import randint
@asyncio.coroutine
def StartState():
print("Start State called \n")
input_value = randint(0, 1)
time.sleep(1)
if (input_value == 0):
result = yield from State2(input_value)
else:
result = yield from State1(input_value)
print("Resume of the Transition: \nStart State calling " \
+ result)
@asyncio.coroutine
def State1(transition_value):
outputValue = str(("State 1 with transition value = %s \n" \
%(transition_value)))
input_value = randint(0, 1)
time.sleep(1)
print("...Evaluating....")
if (input_value == 0):
result = yield from State3(input_value)
else:
result = yield from State2(input_value)
result = "State 1 calling " + result
return (outputValue + str(result))
@asyncio.coroutine
def State2(transition_value):
outputValue = str(("State 2 with transition value = %s \n" \
%(transition_value)))
input_value = randint(0, 1)
time.sleep(1)
print("...Evaluating....")
if (input_value == 0):
result = yield from State1(input_value)
else:
result = yield from State3(input_value)
result = "State 2 calling " + result
return (outputValue + str(result))
@asyncio.coroutine
def State3(transition_value):
outputValue = str(("State 3 with transition value = %s \n" \
%(transition_value)))
input_value = randint(0, 1)
time.sleep(1)
print("...Evaluating....")
if (input_value == 0):
result = yield from State1(input_value)
else:
result = yield from EndState(input_value)
result = "State 2 calling " + result
return (outputValue + str(result))
@asyncio.coroutine
def EndState(transition_value):
outputValue = str(("End State with transition value = %s \n" \
%(transition_value)))
print("...Stop Computation...")
return (outputValue)
if __name__ == "__main__":
print("Finite State Machine simulation With Asyncio Coroutine")
loop = asyncio.get_event_loop()
loop.run_until_complete(StartState())

使用Asyncio的Coroutine来实现一个有限状态机的更多相关文章
- 一个有趣的小例子,带你入门协程模块-asyncio
一个有趣的小例子,带你入门协程模块-asyncio 上篇文章写了关于yield from的用法,简单的了解异步模式,[https://www.cnblogs.com/c-x-a/p/10106031. ...
- Python高级编程之生成器(Generator)与coroutine(四):一个简单的多任务系统
啊,终于要把这一个系列写完整了,好高兴啊 在前面的三篇文章中介绍了Python的Python的Generator和coroutine(协程)相关的编程技术,接下来这篇文章会用Python的corout ...
- python:Asyncio模块处理“事件循环”中的异步进程和并发执行任务
python模块Asynico提供了管理事件.携程.任务和线程的功能已经编写并发代码的同步原语. 组成模块: 事件循,Asyncio 每个进程都有一个事件循环. 协程,子例程概念的泛化,可以暂停任务, ...
- Python标准模块--asyncio
1 模块简介 asyncio模块作为一个临时的库,在Python 3.4版本中加入.这意味着,asyncio模块可能做不到向后兼容甚至在后续的Python版本中被删除.根据Python官方文档,asy ...
- 【Unity3D基础教程】给初学者看的Unity教程(五):详解Unity3D中的协程(Coroutine)
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 为什么需要协程 在游戏中有许多过程(Proc ...
- python asyncio笔记
1.什么是coroutine coroutine,最早我是在lua里面看到的,coroutine最大的好处是可以保存堆栈,让程序得以继续执行,在python里面,一般是利用yield来实现,具体可以看 ...
- 【译】深入理解python3.4中Asyncio库与Node.js的异步IO机制
转载自http://xidui.github.io/2015/10/29/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3python3-4-Asyncio%E5%BA%93% ...
- Coroutine,你究竟干了什么?
一 引子 使用Unity已经有一段时间了,对于Component.GameObject之类的概念也算是有所了解,而脚本方面从一开始就选定了C#,目前来看还是挺明智的:Boo太小众,而且支持有限:JS( ...
- asyncio
一.简介 asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用, ...
随机推荐
- LED notification in Android device
Code can control the LED notification in Android device, using android.app.Notification: 1 2 3 4 5 6 ...
- django debug
django_debug_toolbar(略). debug toolbar还不够用,看下面. 1. 在对应的位置设置断点 import pdb pdb.set_trace() 2. runserve ...
- CentOS用yum安装、配置MariaDB
.创建/etc/yum.repos.d/MariaDB.repo文件,这里用到了刚刚发布正式版的10. [mariadb] name = MariaDB baseurl = http://yum.ma ...
- 【SpringBoot】SpringBoot 入门示例
参考资料: http://www.tuicool.com/articles/mqeee2A http://www.cnblogs.com/suncj/p/4065589.html http://spr ...
- Myeclipse8.5 反编译插件 jad 安装
准备工作 下载jad.exe文件和下载jadeclipse插件:http://pan.baidu.com/s/1pJKjVwn JadClipse 官网:http://jadclipse.source ...
- word to word
Question: For each word, you can get a list of neighbor words by calling getWords(String), find all ...
- poj 1789
http://poj.org/problem?id=1789 这是一道图论的题,个人觉得和那个POJ1258是差不多的,就是多了一步,题目难以读懂 题目的意思:就是给你一群字符串要你找字符串对应的字符 ...
- 提高AdoQuery的速度
用TDataSet及其派生类如TAdoQuery对数据库进行查找时, 如果TDataSet类 没有与数据感知控件相连,通过调用DisableControls可以极大地提高查询速度 特别是在数据比较多的 ...
- codeforces 507B. Painting Pebbles 解题报告
题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...
- bootstrap添加时间控件
$('#startTime').daterangepicker({ singleDatePicker: true,format:"YYYY-MM-DD HH:mm:ss",time ...