event loop.
.. versionchanged:: 6.3
- ``make_current=False`` is now the default when creating an IOLoop -
+ ``make_current=True`` is now the default when creating an IOLoop -
previously the default was to make the event loop current if there wasn't
- already a current one. Passing ``make_current=True`` is deprecated.
+ already a current one.
"""
# These constants were originally based on constants from the epoll module.
Setting and clearing the current event loop through Tornado is
deprecated. Use ``asyncio.set_event_loop`` instead if you need this.
"""
+ warnings.warn(
+ "make_current is deprecated; start the event loop first",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ self._make_current()
+
+ def _make_current(self):
# The asyncio event loops override this method.
raise NotImplementedError()
return AsyncIOLoop
- def initialize(self, make_current: bool = False) -> None:
+ def initialize(self, make_current: bool = True) -> None:
if make_current:
- warnings.warn(
- "IOLoop(make_current=True) is deprecated",
- DeprecationWarning,
- stacklevel=2,
- )
- current = IOLoop.current(instance=False)
- # AsyncIO loops can already be current by this point.
- if current is not None and current is not self:
- raise RuntimeError("current IOLoop already exists")
- self.make_current()
+ self._make_current()
def close(self, all_fds: bool = False) -> None:
"""Closes the `IOLoop`, freeing any resources used.
def initialize(self, **kwargs: Any) -> None: # type: ignore
super().initialize(asyncio.get_event_loop(), **kwargs)
- def make_current(self) -> None:
+ def _make_current(self) -> None:
# AsyncIOMainLoop already refers to the current asyncio loop so
# nothing to do here.
pass
self._clear_current()
super().close(all_fds=all_fds)
- def make_current(self) -> None:
- warnings.warn(
- "make_current is deprecated; start the event loop first",
- DeprecationWarning,
- stacklevel=2,
- )
+ def _make_current(self):
if not self.is_current:
try:
self.old_asyncio = asyncio.get_event_loop()
def test_force_current(self):
self.io_loop = IOLoop(make_current=True)
self.assertIs(self.io_loop, IOLoop.current())
- with self.assertRaises(RuntimeError):
- # A second make_current=True construction cannot succeed.
- IOLoop(make_current=True)
- # current() was not affected by the failed construction.
- self.assertIs(self.io_loop, IOLoop.current())
class TestIOLoopCurrentAsync(AsyncTestCase):