ЕЧ
Size: a a a
ЕЧ
ЕЧ
БС
contextvars. Я ожидаю, что в новой таске, у меня будет другой контекст, а по факту он наследуетсяimport asyncio
import contextvars
context_var = contextvars.ContextVar("aa")
async def work(x):
val = context_var.get("no value")
t_id = id(asyncio.Task.current_task())
print(f"{x} - {val} {t_id}")
async def main_async(use_new_context=False):
print(f"Check with {'new context' if use_new_context else 'same context'}")
context_var.set(42)
futures = []
context = contextvars.Context()
for i in range(3):
if use_new_context:
fut = context.run(asyncio.ensure_future, work(i))
else:
fut = asyncio.ensure_future(work(i))
futures.append(fut)
context_var.set(i)
await asyncio.wait(futures)
loop = asyncio.get_event_loop()
loop.run_until_complete(main_async(False))
loop.run_until_complete(main_async(True))
Check with same context
0 - 42 139670776814792
1 - 0 139670776814952
2 - 1 139670776815112
Check with new context
0 - no value 139670776815112
1 - no value 139670776814792
2 - no value 139670776814952
RH
RH
💭П
SS
💭П
SS
SS
NK

💭П

SS
💭П
ЕЧ
💭П
SA
💭П
AG
AM