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. .net core 微服务通讯组件Orleans的使用与配置

    Orleans非常好用 并且支持.net core 社区也非常活跃 Orleans2.0+在国内的教程偏少 多数是1.5版本的教程 在这里写上四篇Orleans教程 目录 1.Orleans的入门教程 ...

  2. flash存储器原理及作用是什么?

    flash存储器的工作原理 flash存储器又称闪存(快闪存储器),是一种电可擦可编程只读存储器的形式,是可以在操作中被多次擦或写,EEPROM与高速RAM成为当前最常用且发展最快的两种存储技术.计算 ...

  3. 学习Python第一天 ---Hello World

    引言 人生苦短,请用 Python(3.+) 越来越多的情况下使用Python语言进行"代码粘合"和"数据分析"变得非常方便,而且Python 在"爬 ...

  4. Vue ---- 表单指令 条件指令 循环指令 分隔符 过滤器 计算属性 监听属性

    目录 案例讲解: 一. 表单指令 1.重点: 补充 2.单选框 3.单一复选框 4.多复选框 二 . 条件指令 v-if/v-show ... v-clock 三 . 循环指令 string arra ...

  5. 稳定易用的 Django 分页库,完善分页功能

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在 通过 Django Pagination 实现简单分页 中,我们实现了一个简单的 ...

  6. SpringCloud分布式配置中心

    一.什么是配置中心 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud c ...

  7. PythonI/O进阶学习笔记_8.python的可迭代对象和迭代器、迭代设计模式

     content: 1.什么是迭代协议 2. 什么是迭代器(Iterator)和可迭代对象(Iterable) 3. 使用迭代器和可迭代对象 4. 创建迭代器和可迭代对象 5. 迭代器设计模式   一 ...

  8. 在 Java 中如何比较日期?

    在 Java 中有多种方法可以比较日期,日期在计算机内部表示为(long型)时间点--自1970年1月1日以来经过的毫秒数.在Java中,Date是一个对象,包含多个用于比较的方法,任何比较两个日期的 ...

  9. 【前端学习】网页tab键的实现 01

    友情提醒:阅读本文需要了解一些基本的html/Css/Javascript知识 前端常用tab键的实现,用到的原理是当点击一个元素时,通过javascript操作css的display属性,达到控制另 ...

  10. MQ报错2009/2085解决方法

    1.1. 响应2009错误 1.1.1.   涉及协议 MQ,调试回放阶段 1.1.2.   错误信息 完成码2原因为2009 1.1.3.   可能原因 远端MQ连接数不足,拒绝连接 1.1.4.  ...