Python's thread-local data and ContextVar
s appear to achieve the same thing (while having slightly different APIs), with the only user-facing difference being that ContextVar
s work with async code (coroutines and asyncio), while thread-local data does not.
Is that truly the only practical difference?
Does that mean any code targeting a runtime >= 3.7 (when ContextVar
was introduced) is better off using ContextVar
everywhere thread-local data might have been used in the past? Or is there a reason to prefer thread-local data still? (Except for scenarios where you specifically want to associate the state with the thread rather than with the context.)