We have a FastAPI endpoint in which we are using ThreadPoolExecutor to execute 4~5 steps (methods).
Now, we have a class, let's say, Debugger. We are calling its methods and using its attributes as class instance only and not creating objects. For example,
Debugger.log_scalar({"threashold": 0.25})
Debugger.log_artifact("max_plot": "some_plot")
These methods add key-values in a dict at class instance of Debugger
Now, different methods in different files in ThreadPoolExecutor call these Debugger methods to update dict at Debugger class instance which is later returned by the FastAPI endpoint.
This setup is working fine for 1 request at a time, But, if we have 2 or more requests at the same time, then, the dict at Debugger class instance would mix-up data for different requests. How can we maintain different dict for each request at Debugger class Instance.
For example, let's say, we create a unique uuid() at the start of FastAPI request, then, how can Debugger know that, it is being called from which request, so that it can keep dict separately for separate requests. Let me know if any other clarification is needed.
So, we could say, the question is, how to pass some value to a Thread and maintain its lineage. Or we could say, how to know the class is accessed from which Thread?
Notes :-
We could pass
uuid()to each method viaThreadPoolExecutor, then, to internal other methods, then to some other methods in other files from where Debugger is called, then toDebugger()class as object. I think this is not a good way to do. In this approach, if a new developer is creating a new step, then he should make sure to passuuid()in all his steps or files and then toDebugger()objectWe tried threading.local().
For example, before ThreadPoolExecutor,
local_data = threading.local() local_data.req_id = uuid()Then, inside Debugger class instance,
local_data = threading.local() current_req_id = local_data.req_idBut, this is giving me the error,
AttributeError: '_thread._local' object has no attribute 'req_id'Also, If I am creating
threading.local()variable at some common file and importing it everywhere, then also the same error. It's not necessary to do withthreading.local(), I am just sharing what all we tried.