Yury Selivanov added the comment:
> my expected result is all objects should be different, because the code runs
> in different context.
Contexts "branch", so if you store something in the context before asyncio.run
it will store it in the current thread state. And then asyncio.run will
New submission from Jason Chen :
import asyncio
from contextvars import *
var = ContextVar('VarTest', default=None)
def get_var(tag=None):
obj = var.get()
if obj is None:
obj = object()
var.set(obj)
print(f'{tag=}: get_var result is {obj=}')
return obj
async