Example #1
0
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)
Example #2
0
 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
Example #3
0
 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
Example #4
0
 def active(self):
     return RequestContextManager.active() and \
         "security_context" in RequestContextManager.get_context()
Example #5
0
 def clear_context(self):
     self._assert_request_context()
     if "security_context" in RequestContextManager.get_context():
         del RequestContextManager.get_context()["security_context"]
Example #6
0
 def _assert_request_context(self):
     if not RequestContextManager.active():
         raise RequestContextError("Authentication provider cannot access request context.")
Example #7
0
 def request_context_scoped(self):
     return RequestContextManager.active()
Example #8
0
 def execution_wrapper():
     # enable RequestContext on current thread
     with StackContext(RequestContextManager(request_ctx).context_manager):
         return fn(*args, **kwargs)
Example #9
0
 def clear_context(self):
     self._assert_request_context()
     if "security_context" in RequestContextManager.get_context():
         del RequestContextManager.get_context()["security_context"]
Example #10
0
 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
Example #11
0
 def request_context_scoped(self):
     return RequestContextManager.active()
Example #12
0
 def get_requests(self):
     access_store = self.__request_store if RequestContextManager.active(
     ) else _local_access_history
     return access_store.history
Example #13
0
 def set_context(self, context):
     self._assert_request_context()
     RequestContextManager.get_context()["security_context"] = context
Example #14
0
 def get_context(self):
     self._assert_request_context()
     return RequestContextManager.get_context().get("security_context",
                                                    None)
Example #15
0
 def active(self):
     return RequestContextManager.active() and \
         "security_context" in RequestContextManager.get_context()
Example #16
0
 def get_context(self):
     self._assert_request_context()
     return RequestContextManager.get_context().get("security_context", None)
Example #17
0
 def set_context(self, context):
     self._assert_request_context()
     RequestContextManager.get_context()["security_context"] = context
Example #18
0
 def _assert_request_context(self):
     if not RequestContextManager.active():
         raise RequestContextError(
             "Authentication provider cannot access request context.")