MCPcopy Create free account
hub / github.com/MemTensor/MemOS / ContextThread

Class ContextThread

src/memos/context/context.py:206–241  ·  view source on GitHub ↗

Thread class that automatically propagates the main thread's trace_id to child threads.

Source from the content-addressed store, hash-verified

204
205
206class ContextThread(threading.Thread):
207 """
208 Thread class that automatically propagates the main thread's trace_id to child threads.
209 """
210
211 def __init__(self, target, args=(), kwargs=None, **thread_kwargs):
212 super().__init__(**thread_kwargs)
213 self.target = target
214 self.args = args
215 self.kwargs = kwargs or {}
216
217 self.main_trace_id = get_current_trace_id()
218 self.main_api_path = get_current_api_path()
219 self.main_env = get_current_env()
220 self.main_user_type = get_current_user_type()
221 self.main_user_name = get_current_user_name()
222 self.main_context = get_current_context()
223
224 def run(self):
225 # Create a new RequestContext with the main thread's trace_id
226 if self.main_context:
227 # Copy the context data
228 child_context = RequestContext(
229 trace_id=self.main_trace_id,
230 api_path=self.main_api_path,
231 env=self.main_env,
232 user_type=self.main_user_type,
233 user_name=self.main_user_name,
234 )
235 child_context._data = self.main_context._data.copy()
236
237 # Set the context in the child thread
238 set_request_context(child_context)
239
240 # Run the target function
241 self.target(*self.args, **self.kwargs)
242
243
244class ContextThreadPoolExecutor(ThreadPoolExecutor):

Callers 15

startMethod · 0.90
initialize_rabbitmqMethod · 0.90
redis_start_listeningMethod · 0.90
get_messagesMethod · 0.90
run_multiple_tasksMethod · 0.90
run_raceMethod · 0.90
start_consumerMethod · 0.90
__init__Method · 0.90

Calls

no outgoing calls