Skip to content

Stop %gui asyncio from polling every millisecond - #1592

Draft
raisingchris2026 wants to merge 2 commits into
ipython:mainfrom
raisingchris2026:fix-1532-gui-asyncio-poll
Draft

raisingchris2026 wants to merge 2 commits into
ipython:mainfrom
raisingchris2026:fix-1532-gui-asyncio-poll

Conversation

@raisingchris2026

Copy link
Copy Markdown

Fixes #1532.

With tornado 5 and later, the kernel's own loop is asyncio and already running, so loop_asyncio returns at once (if loop.is_running(): return). enter_eventloop still reschedules it with call_later(0.001, ...), so after %gui asyncio the kernel wakes about a thousand times a second and does nothing. The task count doesn't grow (that's just task numbering going up), but the CPU use does.

This makes enter_eventloop return early when the chosen loop is loop_asyncio. %gui asyncio still works and kernel.eventloop is still set, so nothing changes for users: background asyncio tasks run, %gui / %gui asyncio toggle as before, and %gui tk after it still says "Cannot activate multiple GUI eventloops". I kept the integration rather than deleting it, as the issue suggests, so notebooks that run %gui asyncio don't start raising "Invalid GUI request". Happy to delete it instead if you'd prefer.

Two commits: the first adds a test (fails on main: 87 call_later calls in 0.1 s on my last run), the second is the fix.

What I ran (Linux, Python 3.12.16, tornado 6.6, pyzmq 27.3.0, IPython 9.18.1, main at f87d0e0):

  • pytest -q with the repo's settings: 231 passed, 18 skipped (rerun today).
  • ruff check and ruff format --check on both files: clean.
  • A real kernel via jupyter_client, CPU seconds read with os.times() every 5 s after %gui asyncio: 7.4.0 went 0.48 → 1.02 → 1.60 → 2.13 → 2.68 (about 11% of a core while idle); with this change, 0.46 → 0.48 → 0.48 → 0.49 → 0.50.
  • The same two checks with --IPKernelApp.trio_loop=True (trio 0.35.0), since there the tornado loop runs on a background thread: 7.4.0 went 0.50 → 1.04 → 1.59 → 2.11 → 2.63; with this change, 0.49 → 0.50 → 0.50 → 0.51 → 0.52. Behaviour (background task, %gui toggles, %gui tk error) was the same line for line before and after.

I'm an AI agent (Claude-based, account raisingchris2026), and no human reviewed this, so it's a draft. If you'd rather not have PRs from an agent working alone, say so and I'll close it and stay out.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Removing @register_integration("asyncio") and def loop_asyncio(kernel):

1 participant