def _handle_error(self, typ, value, tb): if not issubclass(typ, (TaskletExit, SystemExit)): traceback.print_exception(typ, value, tb) if issubclass(typ, (KeyboardInterrupt, SystemExit, SystemError)): current = get_current() assert current is self.tasklet self.tasklet.parent.throw(typ, value)
def _run(self, forever): current = get_current() if current is not self.tasklet.parent: raise RuntimeError('run() can only be called from MAIN tasklet') if self.tasklet.dead: raise RuntimeError('event loop has already ended') if self._started: raise RuntimeError('event loop was already started') self._started = True self.tasklet.switch(forever=forever)
def _run(self, forever): current = get_current() if current is not self.tasklet.parent: raise RuntimeError("run() can only be called from MAIN tasklet") if self.tasklet.dead: raise RuntimeError("event loop has already ended") if self._started: raise RuntimeError("event loop was already started") self._started = True self.tasklet.switch(forever=forever)
def switch(self): if not self._started: self._run(forever=False) return current = get_current() assert current is not self.tasklet, 'Cannot switch to MAIN from MAIN' try: if self.tasklet.parent is not current: current.parent = self.tasklet except ValueError: pass # gets raised if there is a tasklet parent cycle return self.tasklet.switch()
def switch(self): if not self._started: self._run(forever=False) return current = get_current() assert current is not self.tasklet, "Cannot switch to MAIN from MAIN" try: if self.tasklet.parent is not current: current.parent = self.tasklet except ValueError: pass # gets raised if there is a tasklet parent cycle return self.tasklet.switch()
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 = get_current() assert loop.tasklet is not current timer = loop.call_later(seconds, current.switch) try: loop.switch() finally: timer.cancel()
def _get_local_dict(): current = get_current() s = '_%s__local_dict__' % current.__class__.__name__ if not hasattr(current, s): setattr(current, s, {}) return getattr(current, s)