diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index 407d8d58..1cf5697b 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -635,36 +635,6 @@ def configure_tornado_logger(self): handler.setFormatter(formatter) logger.addHandler(handler) - def _init_asyncio_patch(self): - """set default asyncio policy to be compatible with tornado - Tornado 6 (at least) is not compatible with the default - asyncio implementation on Windows - Pick the older SelectorEventLoopPolicy on Windows - if the known-incompatible default policy is in use. - Support for Proactor via a background thread is available in tornado 6.1, - but it is still preferable to run the Selector in the main thread - instead of the background. - do this as early as possible to make it a low priority and overridable - ref: https://github.com/tornadoweb/tornado/issues/2608 - FIXME: if/when tornado supports the defaults in asyncio without threads, - remove and bump tornado requirement for py38. - Most likely, this will mean a new Python version - where asyncio.ProactorEventLoop supports add_reader and friends. - """ - if sys.platform.startswith("win"): - import asyncio - - try: - from asyncio import WindowsProactorEventLoopPolicy, WindowsSelectorEventLoopPolicy - except ImportError: - pass - # not affected - else: - if type(asyncio.get_event_loop_policy()) is WindowsProactorEventLoopPolicy: - # WindowsProactorEventLoopPolicy is not compatible with tornado 6 - # fallback to the pre-3.8 default of Selector - asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy()) - def init_pdb(self): """Replace pdb with IPython's version that is interruptible. @@ -684,7 +654,6 @@ def init_pdb(self): @catch_config_error def initialize(self, argv=None): """Initialize the application.""" - self._init_asyncio_patch() super().initialize(argv) if self.subapp is not None: return diff --git a/tests/conftest.py b/tests/conftest.py index 49e38c7c..54a644da 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -import asyncio import logging import os from math import inf @@ -42,11 +41,6 @@ resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard)) -# Enforce selector event loop on Windows. -if os.name == "nt": - asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type:ignore - - class TestSession(Session): """A session that copies sent messages to an internal stream, so that they can be accessed later. diff --git a/tests/test_async.py b/tests/test_async.py index c2dd980b..ac935a52 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -4,6 +4,7 @@ import time import pytest +import zmq from .test_message_spec import validate_message from .utils import TIMEOUT, execute, flush_channels, start_new_kernel @@ -11,23 +12,35 @@ KC = KM = None -@pytest.fixture(autouse=True) -def _setup_env(): - """start the global kernel (if it isn't running) and return its client""" - global KM, KC - KM, KC = start_new_kernel() - flush_channels(KC) - yield - assert KC is not None - assert KM is not None - KC.stop_channels() - KM.shutdown_kernel(now=True) +async def test_context(): + context = zmq.Context() + a, b = Socket(context, zmq.PAIR), Socket(context, zmq.PAIR) + port = a.bind_to_random_port("tcp://127.0.0.1") + b.connect(f'tcp://127.0.0.1:{port}') + a.send(b"Hello") + assert b.recv() == b"Hello" + async with a, b: + await a.asend(b"Hello") + assert await b.arecv() == b"Hello" -def test_async_await(): - flush_channels(KC) - msg_id, content = execute("import asyncio; await asyncio.sleep(0.1)", KC) - assert content["status"] == "ok", content +# @pytest.fixture(autouse=True) +# def _setup_env(): +# """start the global kernel (if it isn't running) and return its client""" +# global KM, KC +# KM, KC = start_new_kernel() +# flush_channels(KC) +# yield +# assert KC is not None +# assert KM is not None +# KC.stop_channels() +# KM.shutdown_kernel(now=True) +# +# +# def test_async_await(): +# flush_channels(KC) +# msg_id, content = execute("import asyncio; await asyncio.sleep(0.1)", KC) +# assert content["status"] == "ok", content @pytest.mark.skipif(os.name == "nt", reason="Cannot interrupt on Windows")