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


Python Dispatcher.cancel方法代码示例

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


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

示例1: __init__

# 需要导入模块: from dispatcher import Dispatcher [as 别名]
# 或者: from dispatcher.Dispatcher import cancel [as 别名]

#.........这里部分代码省略.........
    Enforces optional state timeouts.
    Can generate internal leave, enter, and timeout events.
    """
    def __init__(self, obj, initial_state: str, state_timeouts: Dict[str,int]=None) -> None:
        super(State, self).__init__()
        self.log = logging.getLogger(self.__class__.__name__)

        if not obj:
            raise TypeError('This state does not have an owner object.')
        self.obj = obj

        if not initial_state:
            raise TypeError('This state does not have an initial state.')
        self.initial_state: str = initial_state

        self.current_state: str = None
        self.state_timeouts: Dict[str,int] = state_timeouts if state_timeouts else {}
        self.state_timer = None
        self.reset_state()

    def reset_state(self) -> None:
        """
        Set the current state to the initial state.
        """
        if not self.initial_state:
            raise TypeError('This state does not have an initial state.')

        self.log.info('Resetting state to ' + str(self.initial_state) + '.')
        self.current_state = self.initial_state
        self.stop_state_timer()

    def identify_state(self, event) -> str: #pylint: disable=unused-argument
        """
        Return the current state as the state variable.
        """

        return str(self.current_state)

    def change_state(self, new_state: str, notify: bool=False) -> None:
        """
        Transition to the new state and optionally notify listeners.
        The state transition will also generate and process internal leave and enter events.
        """

        if self.current_state is None:
            raise Exception('This state does not have a current state.')

        old_state = self.current_state

        # Only execute block if the state is really changing.
        if old_state != new_state:
            # Stop state timer.
            self.stop_state_timer()

            # Leave pseudo-event.
            Dispatcher().send(StateEvent(self.EVENT_LEAVE, new_state=new_state, old_state=old_state), self.obj, self.obj)

            # Change state.
            self.log.info('Changing state from %s to %s.' % (str(old_state), str(new_state)))
            self.current_state = new_state

            # Enter pseudo-event.
            Dispatcher().send(StateEvent(self.EVENT_ENTER, new_state=new_state, old_state=old_state), self.obj, self.obj)

            # Notify listeners.
            if notify:
                self.log.info('Notify listeners of the state change.')
                Dispatcher().notify(StateEvent(self.EVENT_STATE_CHANGE, new_state=new_state, old_state=old_state), self.obj)

            # Start state timer.
            self.start_state_timer()

    def start_state_timer(self) -> None:
        """
        If the current state is configured with a state timeout, then start the state timer.
        """

        state_timeout = self.state_timeouts.get(self.current_state, None)

        if state_timeout:
            self.log.info('Start state timer with a timeout of ' + str(state_timeout) + '.')

            if self.state_timer:
                self.stop_state_timer()

            self.state_timer = Dispatcher().schedule(state_timeout, StateEvent(self.EVENT_TIMEOUT, new_state=self.current_state), self.obj, self.obj)

    def stop_state_timer(self) -> None:
        """
        Stop the state timer if it is running.
        """

        if self.state_timer:
            self.log.info('Stop state timer.')

            self.state_timer.cancel()
            self.state_timer = None

    def __str__(self):
        return 'state: %s, timer: %s, timeouts: %s' % (str(self.current_state), str(self.state_timer), str(self.state_timeouts))
开发者ID:CaveMike,项目名称:iron,代码行数:104,代码来源:state.py


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