python模拟进程状态

代码如下

from transitions import Machine

定义一个自己的类

class Matter(object):
pass
model = Matter()

状态定义

states=['New','Ready','Waiting','Running','Terminated']

定义状态转移

The trigger argument defines the name of the new triggering mothod

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) #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 19:52  20201224吴卓航  阅读(86)  评论(0编辑  收藏  举报