如图:

#!/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来实现一个有限状态机的更多相关文章

  1. 一个有趣的小例子,带你入门协程模块-asyncio

    一个有趣的小例子,带你入门协程模块-asyncio 上篇文章写了关于yield from的用法,简单的了解异步模式,[https://www.cnblogs.com/c-x-a/p/10106031. ...

  2. Python高级编程之生成器(Generator)与coroutine(四):一个简单的多任务系统

    啊,终于要把这一个系列写完整了,好高兴啊 在前面的三篇文章中介绍了Python的Python的Generator和coroutine(协程)相关的编程技术,接下来这篇文章会用Python的corout ...

  3. python:Asyncio模块处理“事件循环”中的异步进程和并发执行任务

    python模块Asynico提供了管理事件.携程.任务和线程的功能已经编写并发代码的同步原语. 组成模块: 事件循,Asyncio 每个进程都有一个事件循环. 协程,子例程概念的泛化,可以暂停任务, ...

  4. Python标准模块--asyncio

    1 模块简介 asyncio模块作为一个临时的库,在Python 3.4版本中加入.这意味着,asyncio模块可能做不到向后兼容甚至在后续的Python版本中被删除.根据Python官方文档,asy ...

  5. 【Unity3D基础教程】给初学者看的Unity教程(五):详解Unity3D中的协程(Coroutine)

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 为什么需要协程 在游戏中有许多过程(Proc ...

  6. python asyncio笔记

    1.什么是coroutine coroutine,最早我是在lua里面看到的,coroutine最大的好处是可以保存堆栈,让程序得以继续执行,在python里面,一般是利用yield来实现,具体可以看 ...

  7. 【译】深入理解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% ...

  8. Coroutine,你究竟干了什么?

    一 引子 使用Unity已经有一段时间了,对于Component.GameObject之类的概念也算是有所了解,而脚本方面从一开始就选定了C#,目前来看还是挺明智的:Boo太小众,而且支持有限:JS( ...

  9. asyncio

    一.简介 asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用, ...

随机推荐

  1. 给dos命令“.bat”文件换图标

    最近客户有个需求:给企业建立一个FTP服务器,并且给不同的部门分配不同的目录和管理权限. 这个好实现!直接安装serv-u,进行一番设置,搞定! 不过客户嫌登陆FTP操作麻烦,输入ip,输入账号什么的 ...

  2. zabbix安装报某些模块未安装上

    执行下 make clean /usr/local/php5/bin/phpize ./configure --with-curl --with-php-config=/usr/local/php5/ ...

  3. centos7 python3.5 下安装paramiko

    centos7 python3.5 下安装paramiko 安装开发包 yum install openssl openssl-devel python-dev -y 安装pip前需要前置安装setu ...

  4. PyQt4自定义事件

    listview控件与updateText 相关联 self.listview.updateText.connect(self.viewlist)   updateText = QtCore.pyqt ...

  5. admin 后台 bs模板

    http://bootsnipp.com/ https://almsaeedstudio.com/preview

  6. python gui之tkinter语法杂记

    随手写的,后续整理. 1. Entry如何调用父类的__init__? 以下错,原因后续分析 super(Your_Entry_Class, self).__init__(self,**kw) 报错: ...

  7. Android Xpose Hook(一)

    实验环境:     Droid4x模拟器 (目前Android版本4.2.2)     Android Studio 1.下载相关工具 XposedInstaller下载 http://repo.xp ...

  8. 转:js清空数组

    方式1,splice 1 2 3 var ary = [1,2,3,4]; ary.splice(0,ary.length); console.log(ary); // 输出 Array[0],空数组 ...

  9. 【Other】最近正在看的

    待看: https://my.oschina.net/yunfound/blog/141222 https://www.zhihu.com/question/22925358 http://study ...

  10. javascript 解析dom字符串

    知识要求:1:熟悉dom结构层次(如childNodes,nodeType,parent,children)等. 2:熟悉jq对象转换js 对象,反之 毕竟不是专业js人.借助第3方框架.其实jq也是 ...