Python模拟进程状态

2021/11/28 7:11:49

本文主要是介绍Python模拟进程状态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

代码

    from transitions import Machine
    # 定义类
    class Matter(object):
        pass
    model = Matter()
    # 状态定义
    states=['New', 'Ready', 'Waiting', 'Running','Terminated']
    # 定义状态转移
    transitions = [
        {'trigger': 'Admitted', 'source': 'New', 'dest': 'Ready' },
        {'trigger': 'Dispatch', 'source': 'Ready', 'dest': 'Running'},
        {'trigger': 'Interrupt', 'source': 'Running', 'dest': 'Ready'},
        {'trigger': 'InputOutputoreventwait', 'source': 'Running', 'dest': 'Waiting'},
        {'trigger': 'InputOutputoreventcompletion', 'source': 'Waiting', 'dest': 'Ready'},
        {'trigger': 'Exit', 'source': 'Running', 'dest': 'Terminated'}]
    # 初始化
    machine = Machine(model=model, states=states, transitions=transitions, initial='New')
    # Test
    print(model.state) # New
    # 状态转变的完整流程
    model.Admitted()
    print(model.state)
    model.Dispatch()
    print(model.state)
    model.Interrupt()
    print(model.state)
    model.Dispatch()
    print(model.state)
    model.InputOutputoreventwait()
    print(model.state)
    model.InputOutputoreventcompletion()
    print(model.state)
    model.Dispatch()
    print(model.state)
    model.Exit()

运行



这篇关于Python模拟进程状态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程