We use contextvar.ContextVar to store the current interpretation context. These variables are thread-local and their value is not inherited by new threads (python/cpython#128555). This causes some surprising behavior when using APIs that call operations from within spawned threads internally.
from concurrent.futures import ThreadPoolExecutor
from effectful.ops.semantics import handler
from effectful.ops.types import Operation
@Operation.define
def x():
return 0
with handler({x: lambda: 1}):
assert x() == 1
with ThreadPoolExecutor(max_workers=1) as executor:
assert executor.submit(x).result() == 1
There's active upstream discussion about this issue:
We use
contextvar.ContextVarto store the current interpretation context. These variables are thread-local and their value is not inherited by new threads (python/cpython#128555). This causes some surprising behavior when using APIs that call operations from within spawned threads internally.There's active upstream discussion about this issue:
ContextVar(thread_inheritable=...). python/cpython#154564