码迷,mamicode.com
首页 > 编程语言 > 详细

python模拟进程状态

时间:2020-12-18 12:42:26      阅读:2      评论:0      收藏:0      [点我收藏+]

标签:input   自己的   att   get   run   初始   stat   event   trigger   

参考:http://www.cnblogs.com/21207-iHome/p/6085334.html
技术图片

代码: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) # 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

python模拟进程状态

标签:input   自己的   att   get   run   初始   stat   event   trigger   

原文地址:https://www.cnblogs.com/jiangcun/p/14128832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!