博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Asyncio的Coroutine来实现一个有限状态机
阅读量:4954 次
发布时间:2019-06-12

本文共 2200 字,大约阅读时间需要 7 分钟。

如图:

#!/usr/bin/env python# -*- coding: utf-8 -*-import asyncioimport datetimeimport timefrom random import randint@asyncio.coroutinedef 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.coroutinedef 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.coroutinedef 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.coroutinedef 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.coroutinedef 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())

转载于:https://www.cnblogs.com/aguncn/p/6124928.html

你可能感兴趣的文章
9、总线
查看>>
Git 笔记 - section 1
查看>>
HDU6409 没有兄弟的舞会
查看>>
2018 Multi-University Training Contest 10 - TeaTree
查看>>
2018 Multi-University Training Contest 10 - Count
查看>>
HDU6203 ping ping ping
查看>>
《人人都是产品经理》书籍目录
查看>>
如何在git bash中运行mysql
查看>>
OO第三阶段总结
查看>>
构建之法阅读笔记02
查看>>
DataTable和 DataRow的 区别与联系
查看>>
检索COM 类工厂中CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败
查看>>
mysql数据库中数据类型
查看>>
Fireworks基本使用
查看>>
两台电脑间的消息传输
查看>>
Linux 标准 I/O 库
查看>>
.net Tuple特性
查看>>
Java基础常见英语词汇
查看>>
iOS并发编程笔记【转】
查看>>
08号团队-团队任务5:项目总结会
查看>>