Contextvar 'value' 在经历 'while' 循环时不会改变。
'''
import contextvars
import keyboard
import asyncio
import random
value = contextvars.ContextVar('value')
value.set('-')
async def q():
while True:
await asyncio.sleep(1)
print(value.get())
async def s():
while True:
x = random.choice(list(range(10)))
value.set(x)
await asyncio.sleep(1)
async def main():
t1 = asyncio.create_task(q())
t2 = asyncio.create_task(s())
await t1
asyncio.run(main())
输出是 '---' 。我想为此上下文 var 设置一个新值,但我找不到任何类似的情况
第一次在这里所以我不知道是否所有图像都显示正确并且不知道在这里粘贴代码所以请帮忙