The contextvars and the chain of asyncio tasks in Python

2024-08-10
In Python, the contextvars is somehow like thread local, but mostly for coroutines. But will the context itself be kept consistent in a chain of asyncio tasks? Let’s find out. contextvars Let’s have a rough review of the contextvars in Python. The contextvars module provides a way to maintain a context across a chain of function calls, natively supported with asyncio. For example import asyncio import contextvars var = contextvars.ContextVar('var', default={}) async def sub2(): print(f'in sub2, {var. Continue reading