ipykernel 7.x does not schedule async background tasks after cell completion
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
调研方向
首先使用 ipykernel 6.3.1 和 7.2.0 运行 issue 中的最小可复现示例,然后比较单元格完成后的异步任务调度。跟踪处理单元格完成和后台 asyncio 任务的 kernel 入口点;完成的标准是确定该行为是有意设计的还是回归,并记录或测试预期行为。
由索引模型根据 Issue 内容生成。
描述
Description
In ipykernel 7.x, async background tasks created during a cell (e.g., via asyncio.ensure_future) do not continue running once the cell completes. The task resumes only after executing another cell. The same code behaves correctly on ipykernel 6.x.
This is not about widget rendering or Output; the symptom reproduces with a pure side effect (file write).
(Original discussion history for context: ipywidgets issue list shows jupyter-widgets/ipywidgets#4012 where this started, but this report is deliberately scoped to ipykernel behavior.)
Minimal Reproducible Example
import asyncio
import ipywidgets as W
from IPython.display import display
from IPython import get_ipython
q = asyncio.Queue()
button = W.Button(description="Click me")
current_cell_parent = get_ipython().get_parent()
def on_click(_):
q.put_nowait("click")
button.on_click(on_click)
async def consumer():
get_ipython().set_parent(current_cell_parent)
while True:
await q.get()
with open("consumer_log.txt", "a") as f:
f.write("GOT click\n")
asyncio.ensure_future(consumer())
display(button)
Observed behavior
With ipykernel==6.3.1, clicking the button immediately creates/updates consumer_log.txt.
With ipykernel==7.2.0 or ipykernel==7.1.0, clicking the button does not create/update the file. The file is only written after running another cell.
Expected Behavior
Async tasks created with asyncio.ensure_future (or similar mechanisms) should continue running after the originating cell finishes, just as they do under ipykernel 6.x.
Note on ipykernel PR #1451
Just as a note to help with context: this issue appears to be different from ipython/ipykernel#1451.
That pull request focused on how background thread output is routed to notebook cells, and the ipykernel 7.1.0 release notes describe it as a fix related to display/output behavior coming from threads.
In the situation described here, the behavior involves async background tasks (e.g., asyncio.ensure_future) not continuing to run once a cell has finished executing. The minimal example uses a simple file write to show that no widget mechanisms or thread‑based output routing are involved.
Question
Is this change in async scheduling behavior intentional in ipykernel 7.x?
If so:
- What is the recommended mechanism for running long‑lived async background tasks in notebooks?
If not:
- This appears to be a regression relative to ipykernel 6.x.
Context
- Python: 3.11.9
- JupyterLab: 4.5.6
- ipywidgets: 8.1.8
- ipykernel: 7.2.0
- 主要语言
- Python
- 星标
- 734
- 派生
- 411
- 平均合并
- 1 天 5 小时
- 30 天内合并 PR
- 8
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ipython/ipykernel 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 72/100
-
难度 4/5 3-5 天 新手友好度 68/100
-
难度 3/5 1-2 天 新手友好度 66/100
-
难度 4/5 3-5 天 新手友好度 35/100
查看 ipython/ipykernel 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 88/100
use-agent-os/agent-os#3314 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
BasedHardware/omi#15662 · 1 条评论 ·
-
documentation help wanted
难度 2/5 1-3 小时 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 62/100
AiursoftWeb/AnduinOS-2#19 ·