def runnable(fn, *args, **kwargs): """ Decorator to make a method into an asynchronous method run on a thread. Only supports threads do to a pickling problem with decorators across process boundaries. In addition, it sets up a cloned RequestContext so that data can be shared across RequestContext boundaries. The thread run within its own RequestContext boundary but has access to the data of the calling request. @runnable def foo(): pass :return: a Future """ # create a cloned copy of the existing RequestContext request_ctx = RequestContextManager.get_context().clone( ) if RequestContextManager.active() else None def execution_wrapper(): # enable RequestContext on current thread with StackContext(RequestContextManager(request_ctx).context_manager): return fn(*args, **kwargs) return Executor().get_executor(threaded=True).submit(execution_wrapper)
def _request_context_scope_func(): request_context = RequestContextManager.get_context() if not request_context.contains_listener( SessionContext._request_context_session_release): request_context.register_listener( SessionContext._request_context_session_release) return request_context.id
def history(self): rc = RequestContextManager.get_context() if self.CONTEXT_KEY not in rc: access_history = list() rc.immutable(self.CONTEXT_KEY, access_history) else: access_history = rc[self.CONTEXT_KEY] return access_history
def active(self): return RequestContextManager.active() and \ "security_context" in RequestContextManager.get_context()
def clear_context(self): self._assert_request_context() if "security_context" in RequestContextManager.get_context(): del RequestContextManager.get_context()["security_context"]
def _assert_request_context(self): if not RequestContextManager.active(): raise RequestContextError("Authentication provider cannot access request context.")
def request_context_scoped(self): return RequestContextManager.active()
def execution_wrapper(): # enable RequestContext on current thread with StackContext(RequestContextManager(request_ctx).context_manager): return fn(*args, **kwargs)
def _request_context_scope_func(): request_context = RequestContextManager.get_context() if not request_context.contains_listener(SessionContext._request_context_session_release): request_context.register_listener(SessionContext._request_context_session_release) return request_context.id
def get_requests(self): access_store = self.__request_store if RequestContextManager.active( ) else _local_access_history return access_store.history
def set_context(self, context): self._assert_request_context() RequestContextManager.get_context()["security_context"] = context
def get_context(self): self._assert_request_context() return RequestContextManager.get_context().get("security_context", None)
def _assert_request_context(self): if not RequestContextManager.active(): raise RequestContextError( "Authentication provider cannot access request context.")