from threading import local _thread_locals = local() def get_current_request(): return getattr(_thread_locals, 'request', None) def set_current_request(request): _thread_locals.request = request def set_current_trace_id(trace_id): _thread_locals.trace_id = trace_id def get_current_trace_id(): return getattr(_thread_locals, 'trace_id', None)