如图:

#!/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. When building php 5.3, if you get the following error:

    buildconf: You need autoconf 2.59 or lower to build this version of PHP. You are currently trying to ...

  2. POJ 1330

    http://poj.org/problem?id=1330 题意:给你一棵树的上的两个点,要你求这两个点的最近的父亲节点. 第一行的是m案例数 第二行给你个N,代表有N-1种父子关系,其中a b,a ...

  3. 如何用 Robotframework 来编写优秀的测试用例

    介绍 这篇文档将会是一篇在「高层面」的怎么用 Robotframework 来编写优秀测试用例的原则.至于如何使用 Robotframework 来与您的待测试系统相作用这样的细节讨论是不包含在这篇文 ...

  4. 基础02 Java 跨平台原理

    1993 , JAVA初衷: 机顶盒 1994 年互联网刚刚兴起,.(高司令\ 高斯林),改造成了面向互联网的计算机语言.java重要特性之 ------- 跨平台(一次编译,到处运行).平台:操作系 ...

  5. 日历插件My97DatePicker的使用

    在开发过程中,我们会经常遇到让用户输入日期的表单,这类表单处理起来也不是太繁琐,就是简单的字符串和日期之间的转换.但是,如果用户不按照已设定的日期格式进行输入,必定会造成不必要的麻烦.为了更好的处理这 ...

  6. iOS开发学习网站汇总

    *本文转自CocoaChina 原文:11 Insanely Great iOS Developers Sites永不止步地向他人学习 我相信,要想从一个"还不错"的人变成一个卓越 ...

  7. ABAP 内表的行列转换-发货通知单-SLIS

    REPORT Z_TEST_COL_TO_ROW. TYPE-POOLS: slis. TABLES: VTTP,LIPS,LIKP,KNA1 ,VTTK. DATA: gd_fieldcat TYP ...

  8. STL_fill()用法

    以前很多次会给某个区间赋值,今天才知道有个函数可以满足这个功能. void fill (ForwardIterator first, ForwardIterator last, const T& ...

  9. SQL键值约束、索引使用

    添加約束的方式: [exec sp_helpconstraint 表名]->可用于查找到表创建的约束 CREATE TABLE stuInfo ( stuName ) NOT NULL,非空約束 ...

  10. C#.NET如何不序列化字段、属性

    当我们使用公开属性以及公开字段时,都可以顺利的被序列化, 01.[Serializable] 02.public class MyClass 03.{ 04.    public int ID; 05 ...