python模拟进程状态

我在参考资料中看到了两种实现方式,都做了尝试

代码1

from transitions import Machine

class Matter:
pass
model = Matter() states = ['New','Ready','Waiting','Running','Terminated'] transitions = [
{'trigger':'Admitted','source':'New','dest':'Ready'},
{'trigger':'Dispath','source':'Ready','dest':'Running'},
{'trigger':'Interrupt','source':'Running','dest':'Ready'},
{'trigger':'Exit','source':'Running','dest':'Terminated'},
{'trigger':'Input/Output or event wait','source':'Running','dest':'Waiting'},
{'trigger':'Input/Output or event completion','source':'Waiting','dest':'Ready'},
] machine = Machine(model = model, states = states, transitions = transitions, initial = 'New') print(model.state) model.Admitted() print(model.state)

代码2

from transitions import Machine
from collections import namedtuple class FsmState:
def enter(self,event,fsm):
pass def exit(self, fsm):
pass class New(FsmState):
def enter(self,event,fsm):
print('New') def exit(self,fsm):
print('Ready') class Ready(FsmState):
def enter(self,event,fsm):
print('Ready') def exit(self,event,fsm):
print('Running') class Running(FsmState):
def enter(self,event,fsm):
print('Running') def exit_1(self,fsm):
print('Ready') def exit_2(self,fsm):
print('Waiting') def exit_3(self,fsm):
print('Terminated') class Waiting(FsmState):
def enter(self,evevt,fsm):
print('Waiting') def exit(self,fsm):
print('Ready') class FsmFinalState(FsmState):
def enter(self,event,fsm):
print('Terminated') class FsmEvent:
pass class Admitted(FsmEvent):
pass class Dispatch(FsmEvent):
pass class Interrupt(FsmEvent):
pass class IO_EW(FsmEvent):
pass class IO_EC(FsmEvent):
pass class Exit(FsmEvent):
pass Transaction = namedtuple('Transaction',['prev_state','event']) class FSM:
def __init__(self,context):
self.context = context
self.state_transaction_table = []
self.global_transaction_table = []
self.current_state = None
self.working_state = FsmState def add_global_transaction(self,event,end_state):
if not issubclass(end_state,FsmFinalState):
raise FsmException('The state should be FsmFinalState')
self.global_transaction_table.append(Transaction(self.working_state,end_state)) #
def add_transaction(self,prev_state, event, next_state):
if issubclass(prev_state, FsmFinalState):
raise FsmException("It's not allowed to add transaction after Final State Node")
self.state_transaction_table.append(Transaction(prev_state, event, next_state)) def process_event(self, event):
for transaction in self.global_transaction_table:
if isinstance(event, transaction.event):
self.current_state = transaction.next_state()
self.current_state.enter(event, self)
self.clear_transaction_table()
return
for transaction in self.state_transaction_table:
if isinstance(self.current_state, transaction.prev_state) and isinstance(event, transaction.event):
self.current_state.exit(self.context)
self.current_state = transaction.next_state()
self.current_state.enter(event, self)
if isinstance(self.current_state, FsmFinalState):
self.clear_transaction_table()
return
raise FsmException("Transaction not found") def clear_transaction_table(self):
self.global_transaction_table = []
self.state_transaction_table = []
self.current_state = None def run(self):
if len(self.state_transaction_table) == 0: return
self.current_state = self.state_transaction_table[0].prev_state()
self.current_state.enter(None, self) def isRunning(self):
return self.current_state is not None def next_state(self, event):
for transaction in self.global_transaction_table:
if isinstance(event, transaction.event):
return transaction.next_state
for transaction in self.state_transaction_table:
if isinstance(self.current_state, transaction.prev_state) and isinstance(event, transaction.event):
return transaction.next_state
return None class FsmException(Exception):
def __init__(self, description):
super().__init__(description) class ZTJ(FSM):
def __init__(self):
super().__init__(None) ztj = ZTJ();
ztj.add_transaction(New, Admitted, Ready)
ztj.add_transaction(Ready, Dispatch, Running)
ztj.add_transaction(Running, Interrupt, Ready)
ztj.add_transaction(Running, IO_EW,Waiting)
ztj.add_transaction(Waiting, IO_EC, Ready)
ztj.add_transaction(Running, Exit, FsmFinalState)
ztj.run() ztj.process_event(Admitted())
ztj.process_event(Dispatch())
ztj.process_event(Interrupt())
ztj.process_event(IO_EW())
ztj.process_event(IO_EC())
ztj.process_event(Exit())

程序报错



查找资料后找到一种错误原因



但是我在定义函数时已经添加了self参数,但是程序仍然报错,不知道该怎么解决

python模拟进程状态的更多相关文章

  1. Python模拟登陆新浪微博

    上篇介绍了新浪微博的登陆过程,这节使用Python编写一个模拟登陆的程序.讲解与程序如下: 1.主函数(WeiboMain.py): import urllib2 import cookielib i ...

  2. 【Python数据分析】Python模拟登录(一) requests.Session应用

    最近由于某些原因,需要用到Python模拟登录网站,但是以前对这块并不了解,而且目标网站的登录方法较为复杂, 所以一下卡在这里了,于是我决定从简单的模拟开始,逐渐深入地研究下这块. 注:本文仅为交流学 ...

  3. Python模拟C++输出流

    看到一Python例子,挺有意思的,用Python模拟C++的输出流OStream.单纯只是玩. 原理: 利用Python __lshift__左移内建函数<<,调用时将输出内容,如果内容 ...

  4. 【py登陆】python模拟登录

    用Python模拟登录网站 前面简单提到了 Python 模拟登录的程序,但是没写清楚,这里再补上一个带注释的 Python 模拟登录的示例程序.简单说一下流程:先用cookielib获取cookie ...

  5. 谈网页游戏外挂之用python模拟游戏(热血三国2)登陆

    看web看多了,想写写页游的外挂,其实原理是一样的,就是端口不一样协议字段你不知道,而这也提高了点技术门槛,看我们来一点一点突破这些门槛,这次我们来用python发包模拟flash的客户端登陆. 以热 ...

  6. python 模拟浏览器

    想用python模拟浏览器访问web的方法测试些东西,有哪几种方法呢? 一类:单纯的访问web,不解析其js,css等. 1. urllib2 #-*- coding:utf-8 -* import ...

  7. 【DataStructure In Python】Python模拟二叉树

    使用Python模拟二叉树的基本操作,感觉写起来很别扭.最近做编译的优化,觉得拓扑排序这种东西比较强多.近期刷ACM,发现STL不会用实在太伤了.决定花点儿时间学习一下STL.Boost其实也很强大. ...

  8. 【DataStructure In Python】Python模拟栈和队列

    用Python模拟栈和队列主要是利用List,当然也可以使用collection的deque.以下内容为栈: #! /usr/bin/env python # DataStructure Stack ...

  9. Python模拟键盘输入和鼠标操作

    Python模拟键盘输入和鼠标操作 一.Python键盘输入模拟: import win32api import win32con win32api.keybd_event(17,0,0,0)  #c ...

随机推荐

  1. 使用 Ocelot 匹配路由的方法匹配路由

    使用 Ocelot 匹配路由的方法匹配路由 Intro 之前我们在 Ocelot 网关的基础上自定义了一个认证授权的 Ocelot 中间件,根据请求的路径和 Method 进行匹配,找到对应的权限配置 ...

  2. 探究UE4网络系列(二)、UE4网络核心类分析

    转载请标明出处:http://www.cnblogs.com/zblade/ 一.概要 前面分析了网络核心的基础类Socket/BSDSocket/SocketSubsystem/SocketSubs ...

  3. JAVA可视化闹钟源码

    概述 一些同学的Java课设有这样一个问题,比较感兴趣就做了一下 功能介绍: 1.可增加闹钟 2.可删除闹钟 3.时间到了响铃 4.关闭闹钟不会丢失闹钟(因为闹钟存储在txt文件中,不会因程序关闭就终 ...

  4. 在MSSQL中的简单数据类型递归

    在某些特定的项目需求中,我们需要实现树状数据结构, 由此,我们需要用递归将数据查询出来. WITH T AS ( SELECT ID,PID FROM TableName WHERE ID=1 UNI ...

  5. java8-Stream流API

    一回顾与说明 经过前面发布的三章java8的博客,你就懂得了我们为什么要用Lamda表达式,Lamda表达式的原理与函数式接口的关系,从Lamda表达式到方法引用和构造引用. 想要学Stream流你必 ...

  6. SpringBoot微服务电商项目开发实战 --- api接口安全算法、AOP切面及防SQL注入实现

    上一篇主要讲了整个项目的子模块及第三方依赖的版本号统一管理维护,数据库对接及缓存(Redis)接入,今天我来说说过滤器配置及拦截设置.接口安全处理.AOP切面实现等.作为电商项目,不仅要求考虑高并发带 ...

  7. 一起学Spring之注解和Schema方式实现AOP

    概述 在上一篇,我们了解了通过实现接口和XML配置的方式来实现AOP,在实现注解方式AOP之前,先了解一下AspectJ.AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP语法,能 ...

  8. Supermap/Cesium 开发心得----获取三维视角的四至范围

    网上目前有两种获取当前Camera的四至范围的方法 方法一    这种方法是最通用的,即使在哥伦布视角(2.5D下依旧能准确获取值) function getCurrentExtent() { // ...

  9. 63-容器在 Weave 中如何通信和隔离?

    上一节我们分析了 Weave 的网络结构,今天讨论 Weave 的连通和隔离特性. 首先在host2 执行如下命令: weave launch 192.168.0.44 这里必须指定 host1 的 ...

  10. mongodb-API

    mongodb-API 连接mongo(该操作一般在初始化时就执行) 出现 由于目标计算机积极拒绝,无法连接的错误时 查看是否进行虚拟机的端口转发 将 /etc/ 目录下的mongodb.conf 文 ...