]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
perf: use wait_select on Windows
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 19 Oct 2022 15:38:20 +0000 (16:38 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 11 Dec 2022 19:53:20 +0000 (19:53 +0000)
On windows, the default selector strategy is select. As a consequence
using the higher level selector object comes with a performance penalty.

psycopg/psycopg/waiting.py

index a784aaab4a00fa7274f44945c885bb619b1e2588..3fb4cf097d5634ac181a0c4564c184805c9a587e 100644 (file)
@@ -214,6 +214,9 @@ async def wait_conn_async(gen: PQGenConn[RV], timeout: Optional[float] = None) -
         return rv
 
 
+# Specialised implementation of wait functions.
+
+
 def wait_select(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) -> RV:
     """
     Wait for a generator using select where supported.
@@ -297,7 +300,16 @@ def wait_epoll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) ->
         return rv
 
 
-if selectors.DefaultSelector is getattr(selectors, "EpollSelector", None):
+# Choose the best wait strategy for the platform.
+#
+# the selectors objects have a generic interface but come with some overhead,
+# so we also offer more finely tuned implementations.
+
+if selectors.DefaultSelector is getattr(selectors, "SelectSelector", None):
+    # On Windows, SelectSelector should be the default.
+    wait = wait_select
+
+elif selectors.DefaultSelector is getattr(selectors, "EpollSelector", None):
     # NOTE: select seems more performing than epoll. It is admittedly unlikely
     # that a platform has epoll but not select, so maybe we could kill
     # wait_epoll altogether(). More testing to do.