Skip to content

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

@NewUserHa

Description

@NewUserHa

In current versions of ipykernel, when you execute in jupyter, vscode jupyter,

%gui asyncio

then after a while, when you execute

import asyncio
asyncio.all_tasks()```

you will get and see like

{<Task pending name='Task-52016' coro=<_async_in_context.<locals>.run_in_context() running at D:\test\ipykernel\ipykernel\utils.py:60> wait_for=<Task pending name='Task-52017' coro=<Kernel.shell_main() running at D:\test\ipykernel\ipykernel\kernelbase.py:621> cb=[Task.task_wakeup()]> cb=[ZMQStream._run_callback.<locals>._log_error() at e:\.pixi\envs\default\Lib\site-packages\zmq\eventloop\zmqstream.py:563]>, 
<Task pending name='Task-5217' coro=<Kernel.shell_main() running at D:\test\ipykernel\ipykernel\kernelbase.py:621> cb=[Task.task_wakeup()]>}

It keeps increasing task count and it wastes your some cpu resource in the background. It's acually running these below codes, it just enters the function the return repeatedly, by self.io_loop.call_later(0.001, advance_eventloop) of enter_eventloop() in kernelbase.py.

@register_integration("asyncio")
def loop_asyncio(kernel):
"""Start a kernel with asyncio event loop support."""
import asyncio
loop = asyncio.get_event_loop()
# loop is already running (e.g. tornado 5), nothing left to do
if loop.is_running():
return
if loop.is_closed():
# main loop is closed, create a new one
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop._should_close = False # type:ignore[attr-defined]
# pause eventloop when there's an event on a zmq socket
def process_stream_events(shell_stream):
"""fall back to main loop when there's a socket event"""
if shell_stream.flush(limit=1):
loop.stop()
shell_stream = get_shell_stream(kernel)
notifier = partial(process_stream_events, shell_stream)
loop.add_reader(shell_stream.getsockopt(zmq.FD), notifier)
loop.call_soon(notifier)
while True:
error = None
try:
loop.run_forever()
except KeyboardInterrupt:
continue
except Exception as e:
error = e
if loop._should_close: # type:ignore[attr-defined]
loop.close()
if error is not None:
raise error
break
@loop_asyncio.exit
def loop_asyncio_exit(kernel):

How it happened:
The @register_integration("asyncio") and def loop_asyncio(kernel): was at the old ages, to drive the asyncio codes that inputed by users, like asyncio.ensure_future(...), when ipykernel was running under IOloop of tornado<6.1 which using the selectorloop or even older type eventloop. It was to make users able to run asyncio codes when the underhood is other things.
The current ipykernel its main loop is already asyncio loop, by IOloop of tornado>6.1. The user inputed asyncio codes already run in the main loop.

Therefore the related codes @register_integration("asyncio") and def loop_asyncio(kernel): could be deleted.

Solution:
related issue: #1469 solving the proactor and selector eventloop within asyncio

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions