From: Thomas Grainger Date: Mon, 20 Jun 2022 09:42:19 +0000 (+0100) Subject: use get_running_loop to choose between call_soon and call_soon_threadsafe X-Git-Tag: v6.2.0~1^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8394f40718e24133a170b3ae57413068ea91bd9c;p=thirdparty%2Ftornado.git use get_running_loop to choose between call_soon and call_soon_threadsafe --- diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index ad1bde093..a5545a047 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -139,15 +139,8 @@ class BaseAsyncIOLoop(IOLoop): IOLoop._ioloop_for_asyncio[asyncio_loop] = self - self._thread_identity = 0 - super().initialize(**kwargs) - def assign_thread_identity() -> None: - self._thread_identity = threading.get_ident() - - self.add_callback(assign_thread_identity) - def close(self, all_fds: bool = False) -> None: self.closing = True for fd in list(self.handlers): @@ -244,10 +237,14 @@ class BaseAsyncIOLoop(IOLoop): timeout.cancel() # type: ignore def add_callback(self, callback: Callable, *args: Any, **kwargs: Any) -> None: - if threading.get_ident() == self._thread_identity: - call_soon = self.asyncio_loop.call_soon - else: + try: + if asyncio.get_running_loop() is self.asyncio_loop: + call_soon = self.asyncio_loop.call_soon + else: + call_soon = self.asyncio_loop.call_soon_threadsafe + except RuntimeError: call_soon = self.asyncio_loop.call_soon_threadsafe + try: call_soon(self._run_callback, functools.partial(callback, *args, **kwargs)) except RuntimeError: