]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
use get_running_loop to choose between call_soon and call_soon_threadsafe
authorThomas Grainger <tagrain@gmail.com>
Mon, 20 Jun 2022 09:42:19 +0000 (10:42 +0100)
committerThomas Grainger <tagrain@gmail.com>
Mon, 20 Jun 2022 09:52:17 +0000 (10:52 +0100)
tornado/platform/asyncio.py

index ad1bde093bff613b1840d4e7a22e95e794a92ea8..a5545a047e83083a8fda5578b23aedc48ffbbcc7 100644 (file)
@@ -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: