code: def scoped_session_decorator(func):
      
  
      @wraps(func)
      def wrapper(*args, **kwargs):
          with sessions_scope(session):
              # The session used in func comes from the funcs globals, but
              # it will be a proxied thread local var from the session
              # registry, and will therefore be identical to the one returned
              # by the context manager above.
              logger.debug("Running worker %s in scoped DB session", func.__name__)
              return func(*args, **kwargs)
  
      return wrapper
docs: Manage contexts and add debugging to db sessions.