模拟进程状态

参考老师的链接了解了transitions是一个由Python实现的轻量级的、面向对象的有限状态机框架

同时自己也参考了另外一个链接对此有了更深的认识http://blog.csdn.net/xgbing/article/details/2784127

源代码为
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')

print(model.state) # New

model.Admitted()
print(model.state) # Ready

model.Dispatch()
print(model.state) # Running

model.InputOutputoreventwait()
print(model.state) # Waiting

model.InputOutputoreventcompletion()
print(model.state) # Ready

model.Dispatch()
print(model.state) # Running

model.Interrupt()
print(model.state) # Ready

model.Dispatch()
print(model.state) # Running

model.Exit()
print(model.state) # Terminated

posted @ 2020-12-13 17:11  20201325my  阅读(63)  评论(0编辑  收藏  举报