def __wrapper__(*args, **kwargs): _stack_context_handle_exception_partial = partial( _stack_context_handle_exception, handler=args[0]) yield run_with_stack_context( stack_context.ExceptionStackContext( _stack_context_handle_exception_partial, delay_warning=True), lambda: method(*args, **kwargs))
def test_run_with_stack_context(self): @gen.coroutine def f1(): self.assertEqual(self.active_contexts, ['c1']) yield run_with_stack_context( StackContext(functools.partial(self.context, 'c1')), f2) self.assertEqual(self.active_contexts, ['c1']) @gen.coroutine def f2(): self.assertEqual(self.active_contexts, ['c1', 'c2']) yield gen.Task(self.io_loop.add_callback) self.assertEqual(self.active_contexts, ['c1', 'c2']) self.assertEqual(self.active_contexts, []) run_with_stack_context( StackContext(functools.partial(self.context, 'c1')), f1) self.assertEqual(self.active_contexts, [])
def test_run_with_stack_context(self): @gen.coroutine def f1(): self.assertEqual(self.active_contexts, ['c1']) yield run_with_stack_context( StackContext(functools.partial(self.context, 'c2')), f2) self.assertEqual(self.active_contexts, ['c1']) @gen.coroutine def f2(): self.assertEqual(self.active_contexts, ['c1', 'c2']) yield gen.Task(self.io_loop.add_callback) self.assertEqual(self.active_contexts, ['c1', 'c2']) self.assertEqual(self.active_contexts, []) run_with_stack_context( StackContext(functools.partial(self.context, 'c1')), f1) self.assertEqual(self.active_contexts, [])
def run_with_new_context(func, tracer_id: str=None, endpoint: str=None, environment: str=None): """ Stacks a new context in the thread's current stack and then run the method ``work``. Does the same as ``new_session`` but in an uglier fashion. Use ``new_session`` if possible. :param func: a function (usually a partial function) with the work to be done. :param tracer_id: an optional tracer id used when logging :param endpoint: name of the endpoint that was requested """ context = GlobalContextManager.prepare_context(tracer_id, endpoint, environment) stack_context = StackContext(partial(global_context_manager.push_context, context)) return run_with_stack_context(stack_context, func)
def wrapper(self, *args, **kwargs): self._auto_finish = False self.token = yield stack_context.run_with_stack_context( stack_context.ExceptionStackContext( self._stack_context_handle_exception), self.validate_token) if not self.token: raise tornado.web.HTTPError(403) with stack_context.ExceptionStackContext( self._stack_context_handle_exception): result = method(self, *args, **kwargs) if isinstance(result, Future): def future_complete(f): f.result() if not self._finished: self.finish() IOLoop.current().add_future(result, future_complete)
def f1(): self.assertEqual(self.active_contexts, ['c1']) yield run_with_stack_context( StackContext(functools.partial(self.context, 'c2')), f2) self.assertEqual(self.active_contexts, ['c1'])
def f1(): self.assertEqual(self.active_contexts, ['c1']) yield run_with_stack_context( StackContext(functools.partial(self.context, 'c2')), f2) self.assertEqual(self.active_contexts, ['c1'])
def _wrapper(*args, **kwargs): request_id = uuid.uuid4().hex yield run_with_stack_context( StackContext(lambda: RequestIDContext(request_id)), lambda: func(*args, **kwargs))
def run(self, target_coroutine): yield stack_context.run_with_stack_context(self(), target_coroutine)
def run(self, target_coroutine): yield stack_context.run_with_stack_context(self(), target_coroutine)