ipython / ipython/ipykernel

ContextVar not preserved when cell contains `await`

未关闭
#749 3 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
734
派生
412
平均合并
1 天 5 小时
30 天内合并 PR
8

描述

See https://gist.github.com/mvolfik/2582939ec00120a04503507214c24105

```python3
import contextvars, asyncio
VAR = contextvars.ContextVar("VAR", default="default")

async def produce_result(x):
return x

async def read_var():
print("Var:", VAR.get())

# The following is broken

# In[3]:
result = await produce_result("bug fixed yayy")
VAR.set(result)

# In[4]:
await read_var()
# Var: default

# The following works

# In[5]:
result = await produce_result("this worked before")

# In[6]:
VAR.set(result)

# In[7]:
await read_var()
# Var: this worked before
```

In the working case, `await` result is stored in a variable, and `VAR` is set from cell which is fully synchronous (idk ipython internals, but that's what I guess makes the difference). What breaks is setting var from the same cell where we call and await the async function.

I'm using `ipykernel==6.2.0`. Neither of these cases worked (both outputted 'default') with `ipykernel==5.5.0`.

Related to ipython issue ipython/ipython#11565, which was fixed by #632

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。