|
6 | 6 | import os |
7 | 7 | import platform |
8 | 8 | import sys |
9 | | -from functools import partial |
10 | 9 |
|
11 | 10 | import zmq |
12 | 11 | from packaging.version import Version as V |
@@ -400,63 +399,11 @@ def loop_cocoa_exit(kernel): |
400 | 399 |
|
401 | 400 | @register_integration("asyncio") |
402 | 401 | def loop_asyncio(kernel): |
403 | | - """Start a kernel with asyncio event loop support.""" |
404 | | - import asyncio |
| 402 | + """Verify the asyncio event loop is supported.""" |
405 | 403 |
|
406 | | - loop = asyncio.get_event_loop() |
407 | | - # loop is already running (e.g. tornado 5), nothing left to do |
408 | | - if loop.is_running(): |
409 | | - return |
410 | | - |
411 | | - if loop.is_closed(): |
412 | | - # main loop is closed, create a new one |
413 | | - loop = asyncio.new_event_loop() |
414 | | - asyncio.set_event_loop(loop) |
415 | | - loop._should_close = False # type:ignore[attr-defined] |
416 | | - |
417 | | - # pause eventloop when there's an event on a zmq socket |
418 | | - def process_stream_events(socket): |
419 | | - """fall back to main loop when there's a socket event""" |
420 | | - loop.stop() |
421 | | - |
422 | | - notifier = partial(process_stream_events, kernel.shell_socket) |
423 | | - loop.add_reader(kernel.shell_socket.getsockopt(zmq.FD), notifier) |
424 | | - loop.call_soon(notifier) |
425 | | - |
426 | | - while True: |
427 | | - error = None |
428 | | - try: |
429 | | - loop.run_forever() |
430 | | - except KeyboardInterrupt: |
431 | | - continue |
432 | | - except Exception as e: |
433 | | - error = e |
434 | | - if loop._should_close: # type:ignore[attr-defined] |
435 | | - loop.close() |
436 | | - if error is not None: |
437 | | - raise error |
438 | | - break |
439 | | - |
440 | | - |
441 | | -@loop_asyncio.exit |
442 | | -def loop_asyncio_exit(kernel): |
443 | | - """Exit hook for asyncio""" |
444 | | - import asyncio |
445 | | - |
446 | | - loop = asyncio.get_event_loop() |
447 | | - |
448 | | - async def close_loop(): |
449 | | - if hasattr(loop, "shutdown_asyncgens"): |
450 | | - yield loop.shutdown_asyncgens() |
451 | | - loop._should_close = True # type:ignore[attr-defined] |
452 | | - loop.stop() |
453 | | - |
454 | | - if loop.is_running(): |
455 | | - close_loop() |
456 | | - |
457 | | - elif not loop.is_closed(): |
458 | | - loop.run_until_complete(close_loop) # type:ignore[arg-type] |
459 | | - loop.close() |
| 404 | + if not kernel.asyncio_event_loop or not kernel.asyncio_event_loop.is_running(): |
| 405 | + msg = "The asyncio event loop is not running so is not supported." |
| 406 | + raise RuntimeError(msg) |
460 | 407 |
|
461 | 408 |
|
462 | 409 | def set_qt_api_env_from_gui(gui): |
|
0 commit comments