Advanced Features
Multithreading in Python
How to trace multi-threaded applications in Python with HoneyHive.
Since many LLM operations tend to be I/O bound, it is often useful to use threads to perform multiple operations at once.
Usually, you’ll use the ThreadPoolExecutor
class from the concurrent.futures
module in the Python standard library, like this:
Unfortunately, this won’t work as you expect and may cause you to see “broken” traces or missing spans.
The reason relies in how OpenTelemetry (which is what we use under the hood for tracing) uses Python’s context to propagate the trace.
You’ll need to explictly propagate the context to the threads: