Пример #1
0
    def await_all_ps(cls, ps_set):
        self = cls.get()
        fb = Fiber.current()

        def _callback(pss):
            self._fibers.append(fb)

        cls.watch_all_ps(ps_set, _callback)
        self._loop_fiber.switch()
Пример #2
0
    def await_all_tasks(cls, tasks):
        self = cls.get()
        fb = Fiber.current()

        def _callback(ts):
            self._fibers.append(fb)

        cls.watch_all_tasks(tasks, _callback)
        self._loop_fiber.switch()
Пример #3
0
 def switch(self):
     if not self._started:
         self.run()
         return
     current = Fiber.current()
     assert current is not self.task, 'Cannot switch to MAIN from MAIN'
     try:
         if self.task.parent is not current:
             current.parent = self.task
     except ValueError:
         pass  # gets raised if there is a Fiber parent cycle
     return self.task.switch()
Пример #4
0
 def switch(self):
     if not self._started:
         self._run(forever=False)
         return
     current = Fiber.current()
     assert current is not self.task, 'Cannot switch to MAIN from MAIN'
     try:
         if self.task.parent is not current:
             current.parent = self.task
     except ValueError:
         pass  # gets raised if there is a Fiber parent cycle
     return self.task.switch()
Пример #5
0
 def run(self, mode=RUN_DEFAULT):
     if Fiber.current() is not self.task.parent:
         raise RuntimeError('run() can only be called from MAIN fiber')
     if not self.task.is_alive():
         raise RuntimeError('event loop has already ended')
     if self._started:
         raise RuntimeError('event loop was already started')
     self._started = True
     self._running = True
     self._run_mode = mode
     try:
         self.task.switch()
     finally:
         self._running = False
Пример #6
0
 def run(self, mode=RUN_DEFAULT):
     if Fiber.current() is not self.task.parent:
         raise RuntimeError('run() can only be called from MAIN fiber')
     if not self.task.is_alive():
         raise RuntimeError('event loop has already ended')
     if self._started:
         raise RuntimeError('event loop was already started')
     self._started = True
     self._running = True
     self._run_mode = mode
     try:
         self.task.switch()
     finally:
         self._running = False
Пример #7
0
def sleep(seconds=0):
    """Yield control to another eligible coroutine until at least *seconds* have
    elapsed.

    *seconds* may be specified as an integer, or a float if fractional seconds
    are desired.
    """
    loop = evergreen.current.loop
    current = Fiber.current()
    assert loop.task is not current
    timer = loop.call_later(seconds, current.switch)
    try:
        loop.switch()
    finally:
        timer.cancel()
Пример #8
0
def sleep(seconds=0):
    """Yield control to another eligible coroutine until at least *seconds* have
    elapsed.

    *seconds* may be specified as an integer, or a float if fractional seconds
    are desired.
    """
    loop = evergreen.current.loop
    current = Fiber.current()
    assert loop.task is not current
    timer = loop.call_later(seconds, current.switch)
    try:
        loop.switch()
    finally:
        timer.cancel()
Пример #9
0
    def await_all_tasks(cls, tasks):
        """
        wait until a set of Tasks is complete.

        Parameters
        ---
        tasks : list of Task
        """
        self = cls._get()
        fb = Fiber.current()

        def _callback():
            self._fibers.append(fb)

        cls.watch_all_tasks(tasks, _callback)
        self._loop_fiber.switch()
Пример #10
0
    def await_task(cls, task):
        """
        wait until a Task is complete.

        Parameters
        ---
        task : Task
        """
        self = cls._get()
        fb = Fiber.current()

        def _callback():
            self._fibers.append(fb)

        cls.watch_task(task, _callback)
        self._loop_fiber.switch()
Пример #11
0
    def await_all_ps(cls, ps_set):
        """
        wait until a set of ParameterSets is complete.
        While waiting, other co-routines are concurrently executed.

        Parameters
        ---
        ps_set : list of ParameterSet
        """
        self = cls._get()
        fb = Fiber.current()

        def _callback():
            self._fibers.append(fb)

        cls.watch_all_ps(ps_set, _callback)
        self._loop_fiber.switch()
Пример #12
0
    def await_ps(cls, ps):
        """
        wait until parameterSet is complete.
        During waiting, other co-routines are concurrently executed.

        Parameters
        ---
        ps : ParameterSet
        """
        self = cls._get()
        fb = Fiber.current()

        def _callback():
            self._fibers.append(fb)

        cls.watch_ps(ps, _callback)
        self._loop_fiber.switch()
Пример #13
0
 def _handle_error(self, typ, value, tb):
     if not issubclass(typ, SystemExit):
         traceback.print_exception(typ, value, tb)
     if issubclass(typ, (KeyboardInterrupt, SystemExit, SystemError)):
         assert Fiber.current() is self.task
         self.task.parent.throw(typ, value, tb)
Пример #14
0
 def _handle_error(self, typ, value, tb):
     if not issubclass(typ, SystemExit):
         traceback.print_exception(typ, value, tb)
     if issubclass(typ, (KeyboardInterrupt, SystemExit, SystemError)):
         assert Fiber.current() is self.task
         self.task.parent.throw(typ, value, tb)