See https://gist.github.com/mvolfik/2582939ec00120a04503507214c24105
importcontextvars, asyncioVAR=contextvars.ContextVar("VAR", default="default")
asyncdefproduce_result(x):
returnxasyncdefread_var():
print("Var:", VAR.get())
# The following is broken# In[3]:result=awaitproduce_result("bug fixed yayy")
VAR.set(result)
# In[4]:awaitread_var()
# Var: default# The following works# In[5]:result=awaitproduce_result("this worked before")
# In[6]:VAR.set(result)
# In[7]:awaitread_var()
# Var: this worked beforeIn 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
See https://gist.github.com/mvolfik/2582939ec00120a04503507214c24105
In the working case,
awaitresult is stored in a variable, andVARis 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') withipykernel==5.5.0.Related to ipython issue ipython/ipython#11565, which was fixed by #632