当前位置: 首页>>代码示例>>Python>>正文


Python Interpreter.mainEventLoop方法代码示例

本文整理汇总了Python中interpreter.Interpreter.mainEventLoop方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.mainEventLoop方法的具体用法?Python Interpreter.mainEventLoop怎么用?Python Interpreter.mainEventLoop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在interpreter.Interpreter的用法示例。


在下文中一共展示了Interpreter.mainEventLoop方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: StateMachine

# 需要导入模块: from interpreter import Interpreter [as 别名]
# 或者: from interpreter.Interpreter import mainEventLoop [as 别名]

#.........这里部分代码省略.........
#    def setIOProcessors(self, dm):
#        dm["_ioprocessors"] = {"scxml" : {"location" : dm["_x"]["self"]},
#                                    "basichttp" : {"location" : dm["_x"]["self"]} }    
    
    
    def _open_document(self, uri):
        if hasattr(uri, "read"):
            return uri.read()
        elif isinstance(uri, basestring) and re.search("<(.+:)?scxml", uri): #"<scxml" in uri:
            self.filename = "<string source>"
            self.filedir = None
            return uri
        else:
            path, search_path = get_path(uri, self.filedir or "")
            if path:
                self.filedir, self.filename = os.path.split(os.path.abspath(path))
                return open(path).read()
            else:
                msg = "No such file on the PYSCXMLPATH"
                self.logger.error(msg + ": '%s'" % uri)
                self.logger.error("PYTHONPATH: '%s'" % search_path)
                raise IOError(errno.ENOENT, msg, uri)
    
    def _start(self):
        self.compiler.instantiate_datamodel()
        self.interpreter.interpret(self.doc)
    
    def _start_invoke(self, invokeid=None):
        self.compiler.instantiate_datamodel()
        self.interpreter.interpret(self.doc, invokeid)
    
    
    def start(self):
        '''Takes the statemachine to its initial state'''
        if not self.interpreter.running:
            raise RuntimeError("The StateMachine instance may only be started once.")
        else:
            doc = os.path.join(self.filedir, self.filename) if self.filedir else ""
            self.logger.info("Starting %s" % doc)
        self._start()
        self.interpreter.mainEventLoop()
    
    def start_threaded(self):
        self._start()
        eventlet.spawn(self.interpreter.mainEventLoop)
        eventlet.greenthread.sleep()
        
    def isFinished(self):
        '''Returns True if the statemachine has reached it 
        top-level final state or was cancelled.'''
        return self.is_finished
    
    def cancel(self):
        '''
        Stops the execution of the StateMachine, causing 
        all the states in the current configuration to execute 
        their onexit blocks. The StateMachine instance now no longer
        accepts events. For clarity, consider using the 
        top-level <final /> state in your document instead.  
        '''
        self.interpreter.running = False
        self.interpreter.externalQueue.put(Event("cancel.invoke.%s" % self.datamodel.get("_sessionid")))
    
    def send(self, name, data={}):
        '''
        Send an event to the running machine. 
        @param name: the event name (string)
        @param data: the data passed to the _event.data variable (any data type)
        '''
        self._send(name, data)
        eventlet.greenthread.sleep()
            
    def _send(self, name, data={}, invokeid = None, toQueue = None):
        self.interpreter.send(name, data, invokeid, toQueue)
        
    def In(self, statename):
        '''
        Checks if the state 'statename' is in the current configuration,
        (i.e if the StateMachine instance is currently 'in' that state).
        '''
        return self.interpreter.In(statename)
            
    
    def on_exit(self, sender, final):
        if sender is self.interpreter:
            self.is_finished = True
            for timer in self.compiler.timer_mapping.values():
                eventlet.greenthread.cancel(timer)
                del timer
            dispatcher.disconnect(self, "signal_exit", self.interpreter)
            dispatcher.send("signal_exit", self, final=final)
    
    
    def __enter__(self):
        self.start_threaded()
        return self
    
    def __exit__(self, exc_type, exc_value, traceback):
        if not self.isFinished():
            self.cancel()
开发者ID:bollwyvl,项目名称:PySCXML,代码行数:104,代码来源:pyscxml.py


注:本文中的interpreter.Interpreter.mainEventLoop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。