| 1 |
# Fork of jupyter-ui-poll |
| 2 |
# Copyright (C) 2023 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of systemtap, and is free software. You can |
| 5 |
# redistribute it and/or modify it under the terms of the GNU General |
| 6 |
# Public License (GPL); either version 2, or (at your option) any |
| 7 |
# later version. |
| 8 |
# |
| 9 |
# TODO: jupyter-ui-poll assumes the ipython kernel, which has an interactive shell |
| 10 |
# while isystemtap does not. So The changes largely consist of working around that. |
| 11 |
# Would probably benefit from adding changes to their repo and remove this file all |
| 12 |
# together: https://github.com/Kirill888/jupyter-ui-poll/blob/develop/jupyter_ui_poll/_poll.py |
| 13 |
import asyncio |
| 14 |
import sys |
| 15 |
import time |
| 16 |
from collections import abc |
| 17 |
from functools import singledispatch |
| 18 |
from inspect import iscoroutinefunction, isawaitable |
| 19 |
from typing import ( |
| 20 |
Any, |
| 21 |
AsyncIterable, |
| 22 |
AsyncIterator, |
| 23 |
Callable, |
| 24 |
Generic, |
| 25 |
Iterable, |
| 26 |
Iterator, |
| 27 |
List, |
| 28 |
Optional, |
| 29 |
Tuple, |
| 30 |
TypeVar, |
| 31 |
Union, |
| 32 |
) |
| 33 |
|
| 34 |
import zmq |
| 35 |
from IPython import get_ipython |
| 36 |
from tornado.queues import QueueEmpty |
| 37 |
|
| 38 |
from jupyter_ui_poll._async_thread import AsyncThread |
| 39 |
|
| 40 |
T = TypeVar("T") |
| 41 |
|
| 42 |
|
| 43 |
class KernelWrapper: |
| 44 |
_current: Optional["KernelWrapper"] = None |
| 45 |
|
| 46 |
def __init__(self, shell, loop, ker=None) -> None: |
| 47 |
if shell and hasattr(shell, "kernel"): |
| 48 |
kernel = shell.kernel |
| 49 |
self._shell = shell |
| 50 |
else: |
| 51 |
kernel = ker |
| 52 |
self._shell = None |
| 53 |
|
| 54 |
self._kernel = kernel |
| 55 |
self._loop = loop |
| 56 |
self._original_parent = ( |
| 57 |
kernel._parent_ident, |
| 58 |
kernel.get_parent() # ipykernel 6+ |
| 59 |
if hasattr(kernel, "get_parent") |
| 60 |
else kernel._parent_header, # ipykernel < 6 |
| 61 |
) |
| 62 |
self._events: List[Tuple[Any, Any, Any]] = [] |
| 63 |
self._backup_execute_request = kernel.shell_handlers["execute_request"] |
| 64 |
self._aproc = None |
| 65 |
|
| 66 |
if iscoroutinefunction(self._backup_execute_request): # ipykernel 6+ |
| 67 |
kernel.shell_handlers["execute_request"] = self._execute_request_async |
| 68 |
else: |
| 69 |
# ipykernel < 6 |
| 70 |
kernel.shell_handlers["execute_request"] = self._execute_request |
| 71 |
|
| 72 |
if shell: |
| 73 |
shell.events.register("post_run_cell", self._post_run_cell_hook) |
| 74 |
|
| 75 |
|
| 76 |
def restore(self): |
| 77 |
if self._backup_execute_request is not None: |
| 78 |
self._kernel.shell_handlers[ |
| 79 |
"execute_request" |
| 80 |
] = self._backup_execute_request |
| 81 |
self._backup_execute_request = None |
| 82 |
|
| 83 |
def _reset_output(self): |
| 84 |
self._kernel.set_parent(*self._original_parent) |
| 85 |
|
| 86 |
def _execute_request(self, stream, ident, parent): |
| 87 |
# store away execute request for later and reset io back to the original cell |
| 88 |
self._events.append((stream, ident, parent)) |
| 89 |
self._reset_output() |
| 90 |
|
| 91 |
async def _execute_request_async(self, stream, ident, parent): |
| 92 |
self._execute_request(stream, ident, parent) |
| 93 |
|
| 94 |
async def replay(self): |
| 95 |
kernel = self._kernel |
| 96 |
self.restore() |
| 97 |
|
| 98 |
sys.stdout.flush() |
| 99 |
sys.stderr.flush() |
| 100 |
shell_stream = getattr( |
| 101 |
kernel, "shell_stream", None |
| 102 |
) # ipykernel 6 vs 5 differences |
| 103 |
|
| 104 |
for stream, ident, parent in self._events: |
| 105 |
kernel.set_parent(ident, parent) |
| 106 |
if kernel._aborting: |
| 107 |
kernel._send_abort_reply(stream, parent, ident) |
| 108 |
else: |
| 109 |
rr = kernel.execute_request(stream, ident, parent) |
| 110 |
if isawaitable(rr): |
| 111 |
await rr |
| 112 |
|
| 113 |
# replicate shell_dispatch behaviour |
| 114 |
sys.stdout.flush() |
| 115 |
sys.stderr.flush() |
| 116 |
if shell_stream is not None: # 6+ |
| 117 |
kernel._publish_status("idle", "shell") |
| 118 |
shell_stream.flush(zmq.POLLOUT) |
| 119 |
else: |
| 120 |
kernel._publish_status("idle") |
| 121 |
|
| 122 |
async def do_one_iteration(self): |
| 123 |
try: |
| 124 |
rr = self._kernel.do_one_iteration() |
| 125 |
if isawaitable(rr): |
| 126 |
await rr |
| 127 |
except QueueEmpty: |
| 128 |
# it's probably a bug in ipykernel, |
| 129 |
# .do_one_iteration() should not throw |
| 130 |
return |
| 131 |
finally: |
| 132 |
# reset stdio back to original cell |
| 133 |
self._reset_output() |
| 134 |
|
| 135 |
def _post_run_cell_hook(self, *args, **kw): |
| 136 |
if self._shell: |
| 137 |
self._shell.events.unregister("post_run_cell", self._post_run_cell_hook) |
| 138 |
self.restore() |
| 139 |
KernelWrapper._current = None |
| 140 |
asyncio.ensure_future(self.replay(), loop=self._loop) |
| 141 |
|
| 142 |
async def _poll_async(self, n=1): |
| 143 |
for _ in range(n): |
| 144 |
await self.do_one_iteration() |
| 145 |
|
| 146 |
async def __aenter__(self): |
| 147 |
return self._poll_async |
| 148 |
|
| 149 |
async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 150 |
pass |
| 151 |
|
| 152 |
def __enter__(self): |
| 153 |
if self._aproc is not None: |
| 154 |
raise ValueError("Nesting not supported") |
| 155 |
self._aproc = AsyncThread() |
| 156 |
return self._aproc.wrap(self._poll_async) |
| 157 |
|
| 158 |
def __exit__(self, exc_type, exc_val, exc_tb): |
| 159 |
if not self._shell: |
| 160 |
self._post_run_cell_hook() |
| 161 |
|
| 162 |
self._aproc.terminate() |
| 163 |
self._aproc = None |
| 164 |
|
| 165 |
@staticmethod |
| 166 |
def get(kernel=None) -> "KernelWrapper": |
| 167 |
if KernelWrapper._current is None: |
| 168 |
KernelWrapper._current = KernelWrapper( |
| 169 |
get_ipython(), asyncio.get_event_loop(), ker=kernel |
| 170 |
) |
| 171 |
return KernelWrapper._current |
| 172 |
|
| 173 |
|
| 174 |
class IteratorWrapperAsync(abc.AsyncIterable, Generic[T]): |
| 175 |
def __init__( |
| 176 |
self, its: AsyncIterable[T], n: int = 1, |
| 177 |
): |
| 178 |
self._its = its |
| 179 |
self._n = n |
| 180 |
|
| 181 |
def __aiter__(self) -> AsyncIterator[T]: |
| 182 |
async def _loop( |
| 183 |
kernel: KernelWrapper, its: AsyncIterable[T], n: int |
| 184 |
) -> AsyncIterator[T]: |
| 185 |
async with kernel as poll: |
| 186 |
async for x in its: |
| 187 |
await poll(n) |
| 188 |
yield x |
| 189 |
|
| 190 |
return _loop(KernelWrapper.get(), self._its, self._n) |
| 191 |
|
| 192 |
|
| 193 |
class IteratorWrapper(abc.Iterable, Generic[T]): |
| 194 |
def __init__( |
| 195 |
self, its: Iterable[T], n: int = 1, |
| 196 |
): |
| 197 |
self._its = its |
| 198 |
self._n = n |
| 199 |
|
| 200 |
def __iter__(self) -> Iterator[T]: |
| 201 |
def _loop(kernel: KernelWrapper, its: Iterable[T], n: int) -> Iterator[T]: |
| 202 |
with kernel as poll: |
| 203 |
try: |
| 204 |
for x in its: |
| 205 |
poll(n) |
| 206 |
yield x |
| 207 |
except GeneratorExit: |
| 208 |
pass |
| 209 |
except Exception as e: |
| 210 |
raise e |
| 211 |
|
| 212 |
return _loop(KernelWrapper.get(), self._its, self._n) |
| 213 |
|
| 214 |
def __aiter__(self) -> AsyncIterator[T]: |
| 215 |
async def _loop( |
| 216 |
kernel: KernelWrapper, its: Iterable[T], n: int |
| 217 |
) -> AsyncIterator[T]: |
| 218 |
async with kernel as poll: |
| 219 |
for x in its: |
| 220 |
await poll(n) |
| 221 |
yield x |
| 222 |
|
| 223 |
return _loop(KernelWrapper.get(), self._its, self._n) |
| 224 |
|
| 225 |
|
| 226 |
def ui_events(kernel=None): |
| 227 |
""" |
| 228 |
Gives you a function you can call to process UI events while running a long |
| 229 |
task inside a Jupyter cell. |
| 230 |
.. code-block:: python |
| 231 |
with ui_events() as ui_poll: |
| 232 |
while some_condition: |
| 233 |
ui_poll(10) # Process upto 10 UI events if any happened |
| 234 |
do_some_more_compute() |
| 235 |
Async mode is also supported: |
| 236 |
.. code-block:: python |
| 237 |
async with ui_events() as ui_poll: |
| 238 |
while some_condition: |
| 239 |
await ui_poll(10) # Process upto 10 UI events if any happened |
| 240 |
do_some_more_compute() |
| 241 |
#. Call ``kernel.do_one_iteration()`` taking care of IO redirects |
| 242 |
#. Intercept ``execute_request`` IPython kernel events and delay their execution |
| 243 |
#. Schedule replay of any blocked ``execute_request`` events when |
| 244 |
cell execution is finished. |
| 245 |
""" |
| 246 |
return KernelWrapper.get(kernel) |
| 247 |
|
| 248 |
|
| 249 |
@singledispatch |
| 250 |
def with_ui_events(its, n: int = 1): |
| 251 |
""" |
| 252 |
Deal with kernel ui events while processing a long sequence |
| 253 |
Iterable returned from this can be used in both async and sync contexts. |
| 254 |
.. code-block:: python |
| 255 |
for x in with_ui_events(some_data_stream, n=10): |
| 256 |
do_things_with(x) |
| 257 |
async for x in with_ui_events(some_data_stream, n=10): |
| 258 |
await do_things_with(x) |
| 259 |
This is basically equivalent to: |
| 260 |
.. code-block:: python |
| 261 |
with ui_events() as poll: |
| 262 |
for x in some_data_stream: |
| 263 |
poll(10) |
| 264 |
do_things_with(x) |
| 265 |
:param its: |
| 266 |
Iterator to pass through, this should be either |
| 267 |
:class:`~collections.abc.Iterable` or :class:`~collections.abc.AsyncIterable` |
| 268 |
:param n: |
| 269 |
Number of events to process in between items |
| 270 |
:returns: |
| 271 |
:class:`~collections.abc.AsyncIterable` when input is |
| 272 |
:class:`~collections.abc.AsyncIterable` |
| 273 |
:returns: |
| 274 |
Object that implements both :class:`~collections.abc.Iterable` and |
| 275 |
:class:`~collections.abc.AsyncIterable` interfaces when input is normal |
| 276 |
:class:`~collections.abc.Iterable` |
| 277 |
""" |
| 278 |
raise TypeError("Expect Iterable[T]|AsyncIterable[T]") |
| 279 |
|
| 280 |
|
| 281 |
@with_ui_events.register(abc.Iterable) |
| 282 |
def with_ui_events_sync(its: Iterable[T], n: int = 1) -> IteratorWrapper[T]: |
| 283 |
return IteratorWrapper(its, n=n) |
| 284 |
|
| 285 |
|
| 286 |
@with_ui_events.register(abc.AsyncIterable) |
| 287 |
def with_ui_events_async(its: AsyncIterable[T], n: int = 1) -> AsyncIterable[T]: |
| 288 |
return IteratorWrapperAsync(its, n=n) |
| 289 |
|
| 290 |
|
| 291 |
def run_ui_poll_loop( |
| 292 |
f: Callable[[], Optional[T]], sleep: float = 0.02, n: int = 1 |
| 293 |
) -> T: |
| 294 |
""" |
| 295 |
Repeatedly call ``f()`` until it returns something other than ``None`` |
| 296 |
while also responding to widget events. |
| 297 |
This blocks execution of cells below in the notebook while still preserving |
| 298 |
interactivity of jupyter widgets. |
| 299 |
:param f: |
| 300 |
Function to periodically call (``f()`` should not block for long) |
| 301 |
:param sleep: |
| 302 |
Amount of time to sleep in between polling (in seconds, 1/50 is the default) |
| 303 |
:param n: |
| 304 |
Number of events to process per iteration |
| 305 |
:returns: |
| 306 |
First non-``None`` value returned from ``f()`` |
| 307 |
""" |
| 308 |
|
| 309 |
def as_iterator( |
| 310 |
f: Callable[[], Optional[T]], sleep: float |
| 311 |
) -> Iterator[Optional[T]]: |
| 312 |
x = None |
| 313 |
while x is None: |
| 314 |
if sleep is not None: |
| 315 |
time.sleep(sleep) |
| 316 |
|
| 317 |
x = f() |
| 318 |
yield x |
| 319 |
|
| 320 |
for x in with_ui_events(as_iterator(f, sleep), n): |
| 321 |
if x is not None: |
| 322 |
break |
| 323 |
|
| 324 |
assert x is not None |
| 325 |
return x |