From 4bca05d11f73e45915573e88838eb94fb7af2abf Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 1 Jan 2021 18:46:22 +0100 Subject: [PATCH] Create the waiting selector only after the first wait Sometimes there is no need to wait at all, if the generator exits on the first result. It happens especially in copy. --- psycopg3/psycopg3/waiting.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/psycopg3/psycopg3/waiting.py b/psycopg3/psycopg3/waiting.py index ba6e005d5..b0f87474b 100644 --- a/psycopg3/psycopg3/waiting.py +++ b/psycopg3/psycopg3/waiting.py @@ -48,9 +48,9 @@ def wait_selector( Consume *gen*, scheduling `fileno` for completion when it is reported to block. Once ready again send the ready state back to *gen*. """ - sel = DefaultSelector() try: s = next(gen) + sel = DefaultSelector() while 1: sel.register(fileno, s) ready = None @@ -78,9 +78,9 @@ def wait_conn(gen: PQGenConn[RV], timeout: Optional[float] = None) -> RV: Behave like in `wait()`, but take the fileno to wait from the generator itself, which might change during processing. """ - sel = DefaultSelector() try: fileno, s = next(gen) + sel = DefaultSelector() while 1: sel.register(fileno, s) ready = None @@ -214,13 +214,14 @@ def wait_epoll( See also: https://linux.die.net/man/2/epoll_ctl """ - epoll = select.epoll() if timeout is None or timeout < 0: timeout = 0 else: timeout = int(timeout * 1000.0) + try: s = next(gen) + epoll = select.epoll() evmask = poll_evmasks[s] epoll.register(fileno, evmask) while 1: -- 2.47.3