Example #1
0
class ProcessPlayer(object):
    """Starts a process, and sets Checkpoints in its lifecycle"""
    def __init__(self, callable_, name="nameless", *args_for_callable, **kwargs_for_callable):
        """
        :param callable_:
        :param str name: The name of this :class:`ProcessStarter`
        :param args_for_callable:
        :param kwargs_for_callable:
        :return:
        """
        self.name = name
        self.music_sheet = None
        self._terminal_checkpoint = ImplicitCheckpoint(self, None)
        self._initial_checkpoint = ImplicitCheckpoint(self, None, before=True)
        self.instrument = InstrumentedThread(
            target=callable_, args=args_for_callable, kwargs=kwargs_for_callable)

    def add_checkpoint_after(self, callable_, name=None):
        """Create and return a checkpoint, set AFTER the callable returns"""
        return Checkpoint(self, callable_, name=name)

    def add_checkpoint_before(self, callable_, name=None):
        """Create and return a checkpoint, set BEFORE the callable returns"""
        return Checkpoint(self, callable_, before=True, name=name)

    def get_terminal_checkpoint(self):
        """Returns a checkpoint that marks the process end"""
        return self._terminal_checkpoint

    def get_initial_checkpoint(self):
        """Return the initial checkpoint, that marks the process beginning"""
        return self._initial_checkpoint

    def play(self, player_checkpoints, baton):
        self.music_sheet = player_checkpoints
        self.instrument.tune(baton, player_checkpoints)
        self.instrument.start()

    def __repr__(self):
        return u"<Player {}>".format(self.name)

    __str__ = __repr__
Example #2
0
 def __init__(self, callable_, name="nameless", *args_for_callable, **kwargs_for_callable):
     """
     :param callable_:
     :param str name: The name of this :class:`ProcessStarter`
     :param args_for_callable:
     :param kwargs_for_callable:
     :return:
     """
     self.name = name
     self.music_sheet = None
     self._terminal_checkpoint = ImplicitCheckpoint(self, None)
     self._initial_checkpoint = ImplicitCheckpoint(self, None, before=True)
     self.instrument = InstrumentedThread(
         target=callable_, args=args_for_callable, kwargs=kwargs_for_callable)