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 摘要。