.. versionchanged:: 5.0
This method also sets the current `asyncio` event loop.
"""
+ old = getattr(IOLoop._current, "instance", None)
+ if old is not None:
+ old.clear_current()
IOLoop._current.instance = self
@staticmethod
if self._stopped:
self._stopped = False
return
- old_current = getattr(IOLoop._current, "instance", None)
- IOLoop._current.instance = self
+ old_current = IOLoop.current(instance=False)
+ if old_current is not self:
+ self.make_current()
self._thread_ident = thread.get_ident()
self._running = True
self._stopped = False
if self._blocking_signal_threshold is not None:
signal.setitimer(signal.ITIMER_REAL, 0, 0)
- IOLoop._current.instance = old_current
+ if old_current is None:
+ IOLoop.clear_current()
+ elif old_current is not self:
+ old_current.make_current()
if old_wakeup_fd is not None:
signal.set_wakeup_fd(old_wakeup_fd)
to refer to this class directly.
"""
def initialize(self, **kwargs):
+ self.is_current = False
loop = asyncio.new_event_loop()
try:
super(AsyncIOLoop, self).initialize(loop, **kwargs)
loop.close()
raise
+ def close(self, all_fds=False):
+ if self.is_current:
+ self.clear_current()
+ super(AsyncIOLoop, self).close(all_fds=all_fds)
+
def make_current(self):
- super(AsyncIOLoop, self).make_current()
- try:
- self.old_asyncio = asyncio.get_event_loop()
- except RuntimeError:
- self.old_asyncio = None
+ if not self.is_current:
+ super(AsyncIOLoop, self).make_current()
+ try:
+ self.old_asyncio = asyncio.get_event_loop()
+ except RuntimeError:
+ self.old_asyncio = None
+ self.is_current = True
asyncio.set_event_loop(self.asyncio_loop)
def _clear_current_hook(self):
- asyncio.set_event_loop(self.old_asyncio)
+ if self.is_current:
+ asyncio.set_event_loop(self.old_asyncio)
+ self.is_current = False
def to_tornado_future(asyncio_future):