使用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())

时间: 2024-08-28 22:01:17

使用Asyncio的Coroutine来实现一个有限状态机的相关文章

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

啊,终于要把这一个系列写完整了,好高兴啊 在前面的三篇文章中介绍了Python的Python的Generator和coroutine(协程)相关的编程技术,接下来这篇文章会用Python的coroutine技术实现一个简单的多任务的操作系统 代码如下,可看注释 1 #-*-coding:utf-8 -*- 2 ''' 3 用Python和coroutine实现一个简单的多任务系统 4 ''' 5 # ##Step 1:Define Tasks###########################

【译】深入理解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%E4%B8%8ENode-js%E7%9A%84%E5%BC%82%E6%AD%A5IO%E6%9C%BA%E5%88%B6/ 译者:xidui原文: http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html 译者前言 如

python asyncio笔记

1.什么是coroutine coroutine,最早我是在lua里面看到的,coroutine最大的好处是可以保存堆栈,让程序得以继续执行,在python里面,一般是利用yield来实现,具体可以看如下文章: http://www.cnblogs.com/tqsummer/archive/2010/12/27/1917927.html python中的yield以及yield from语法可以让程序支持coroutine 2.asyncio库 Python3中,提供了基于coroutine的异

【Unity3D基础教程】(五):详解Unity3D中的协程(Coroutine)

[狗刨学习网] 为什么需要协程 在游戏中有许多过程(Process)需要花费多个逻辑帧去计算. 你会遇到"密集"的流程,比如说寻路,寻路计算量非常大,所以我们通常会把它分割到不同的逻辑帧去进行计算,以免影响游戏的帧率. 你会遇到"稀疏"的流程,比如说游戏中的触发器,这种触发器大多数时候什么也不做,但是一旦被调用会做非常重要的事情(比图说游戏中自动开启的门就是在门前放了一个Empty Object作为trigger,人到门前就会触发事件). 不管什么时候,如果你想创建

asyncio

一.简介 asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程扔到EventLoop中执行,就实现了异步IO 此模块为编写单线程并发代码提高基础架构,通过使用协程.套接字和其他资源的 I/O 多路复用,运行网络客户端和服务器,以及其他相关的基元. 包内容的详细的列表如下: 各种系统具体实现的可插拔 event loop transport 

python asyncio 协程使用 (一)

由于脚本需要在完成事件处理后N秒检查事件处理结果,当执行失败时再执行另一个事件处理. 想要最小化完成这个功能.同时在第一时间就将执行完毕的结果反馈给接口. 因此想到使用协程. 使用之前先翻阅了一下现有的文档.以及参考了其他人的代码. 先改写成如下的用例: 1 import asyncio 2 3 async def do_some_work(x): 4 try: 5 return "success" 6 finally: 7 print('it can test') 8 await a

python学习笔记 异步asyncio

asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程扔到EventLoop中执行,就实现了异步IO. 用asyncio实现Hello world代码如下: import asyncio @asyncio.coroutine def hello(): print("Hello world!") # 异步调用asyncio.sleep

六十四 asyncio

asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程扔到EventLoop中执行,就实现了异步IO. 用asyncio实现Hello world代码如下: import asyncio @asyncio.coroutine def hello(): print("Hello world!") # 异步调用asyncio.sleep

[Python 多线程] asyncio (十六)

asyncio 该模块是3.4版本加入的新功能. 先来看一个例子: def a(): for x in range(3): print('a.x', x) def b(): for x in 'abc': print('b.x', x) a() b() #运行结果: a.x 0 a.x 1 a.x 2 b.x a b.x b b.x c 这个例子是一个典型的串行程序,两个函数调用是在主线程中顺序执行. 有以下几种方法可以让这段程序改为并行: 1. 生成器 2. 多线程 3. 多进程 4. 协程