MCPcopy Create free account
hub / github.com/dask/dask / test_contextvars

Function test_contextvars

dask/tests/test_threaded.py:218–235  ·  view source on GitHub ↗

When the thread pool is cold, contextvars area automatically propagated from the caller thread to **new** threads on Python 3.14t by default, as well as on 3.14 with the opt-in flag `python -X thread_inherit_context=1`. Test that contextvars are always propagated to the tasks, even in a

(num_workers)

Source from the content-addressed store, hash-verified

216
217@pytest.mark.parametrize("num_workers", [None, 1])
218def test_contextvars(num_workers):
219 """When the thread pool is cold, contextvars area automatically propagated from the
220 caller thread to **new** threads on Python 3.14t by default, as well as on 3.14 with
221 the opt-in flag `python -X thread_inherit_context=1`.
222
223 Test that contextvars are always propagated to the tasks, even in absence of the above
224 flag and even when the threads were already warm when the context was set.
225 """
226 warm_up_thread_pool(num_workers)
227
228 dsk = {"x": (test_ctxvar.get,)}
229 # Reuse thread pool started by warm_up_thread_pool
230 assert get(dsk, "x", num_workers=num_workers) == 42
231 tok = test_ctxvar.set(43)
232 try:
233 assert get(dsk, "x", num_workers=num_workers) == 43
234 finally:
235 test_ctxvar.reset(tok)
236
237
238@pytest.mark.parametrize("num_workers", [None, 1])

Callers

nothing calls this directly

Calls 3

getFunction · 0.90
warm_up_thread_poolFunction · 0.85
setMethod · 0.80

Tested by

no test coverage detected