Variant ThreadPoolExecutor that propagates contextvars from the submitting thread to the worker threads. With a vanilla ThreadPoolExecutor, contextvars are automatically propagated on CPython 3.14t (noGIL) if and only if they are set before the worker threads are started. This v
| 37 | |
| 38 | |
| 39 | class ContextAwareThreadPoolExecutor(ThreadPoolExecutor): |
| 40 | """Variant ThreadPoolExecutor that propagates contextvars |
| 41 | from the submitting thread to the worker threads. |
| 42 | |
| 43 | With a vanilla ThreadPoolExecutor, contextvars are automatically propagated on |
| 44 | CPython 3.14t (noGIL) if and only if they are set before the worker threads are |
| 45 | started. |
| 46 | This variant propagates contextvars on all Python interpreters and also when the |
| 47 | worker threads are already warm when the variables are set in the submitting thread. |
| 48 | |
| 49 | This also affects catching warnings, which on 3.14t use contextvars. |
| 50 | |
| 51 | See Also |
| 52 | -------- |
| 53 | https://docs.python.org/3/using/cmdline.html#envvar-PYTHON_THREAD_INHERIT_CONTEXT |
| 54 | https://docs.python.org/3/using/cmdline.html#envvar-PYTHON_CONTEXT_AWARE_WARNINGS |
| 55 | """ |
| 56 | |
| 57 | def submit(self, fn, /, *args, **kwargs): |
| 58 | ctx = contextvars.copy_context() |
| 59 | return super().submit(ctx.run, fn, *args, **kwargs) |
| 60 | |
| 61 | |
| 62 | def get( |
no outgoing calls
no test coverage detected
searching dependent graphs…