如图:

#!/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. 单链表的类的c++实现

    #include<iostream> using namespace std;template <class T>struct linkNode{ T data; linkNo ...

  2. mysql 字符集设置方法

    php 5.6,mysql 5.5/etc/my.cnf (1) 最简单的修改方法,就是修改mysql的my.ini文件中的字符集键值, 如 default-character-set = utf8c ...

  3. busybox microcom

    /************************************************************************* * busybox microcom * 说明: ...

  4. 试用vSphere 6(三):安装vCenter 6(独立数据库)之:vCenter安装与配置

    ------------------------------------------ 一.VMware vSphere 6(RC版)安装配置系列文章: 1.试用vSphere 6(一):安装ESXi ...

  5. 项目:BluetoothChat

    代码在github: https://github.com/Viyu/BluetoothChat 蓝牙聊天核心是Android Demo里的,我加上了类似微信的界面. 我觉得这个应用要是能推广的话,有 ...

  6. 搭建JavaWeb服务器

    JDK安装可以参考 http://www.cnblogs.com/a2211009/p/4265225.html Tomcat安装可参考 1.由于服务器配置比较低综合考虑,选择ubuntu系统进行搭建 ...

  7. 1. dex和Jar反编译对比

    Java源码 public class Hello { public int foo(int a,int b) { return (a + b) * (a - b); } public static ...

  8. vs2013 内置IIS Express相关问题

    问题描述,以前做的程序迁移到vs2013后出现500.22问题. HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设 ...

  9. WCF 定制自己的签名验证逻辑

    关键点: 1. 保证在客户端设置签名. client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentU ...

  10. POJ 1330

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