Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qvPi5-0052FB-Jl for psycopg@arkaria.postgresql.org; Tue, 24 Oct 2023 22:17:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qvPi2-00Ds4e-Pz for psycopg@arkaria.postgresql.org; Tue, 24 Oct 2023 22:17:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qvPi2-00Ds3X-IM for psycopg@lists.postgresql.org; Tue, 24 Oct 2023 22:17:10 +0000 Received: from mail-oa1-x32.google.com ([2001:4860:4864:20::32]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qvPhv-0028fv-N4 for psycopg@postgresql.org; Tue, 24 Oct 2023 22:17:09 +0000 Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-1e9c42fc0c9so2680610fac.1 for ; Tue, 24 Oct 2023 15:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698185822; x=1698790622; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=y6y4vpntxiPX0FxfdkhKMYUa377+K1x/4jyDYmhBZaE=; b=iFqrWqgZXiwZ76nUgX916qw2LkS580k9SwyxeXvWtf9PaeyLEGRVZpslQCLZmZhDbZ lr9VywJQ7vxF9GMdWTkelXxqCc0+fVssYkui1ZRG+0aPDUHRYSBY5iYGX2pqQ2RV/y/3 gXfjJdA0t9O9ie7sk09gQj2aeYKUrLr0DT0holuloydyTLqh9pq1A7r/gVOVGDXgZejr ToU4pljqAqBlrAHFApLjDeaYqf1E6mv8FrcpaXaWdhUY53kiUR5rVFgegbIhT70fGwYO J2Iz+c0jNwKCYe+W+6qkCROwr35oQgPg3IIds86XIDH+XsRuRQkjL9D7lnZLwIAB8abD BlkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698185822; x=1698790622; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=y6y4vpntxiPX0FxfdkhKMYUa377+K1x/4jyDYmhBZaE=; b=U9b9l/+yR36eGK9Pe812zVyA1IpnUwtjVUIysyLlC+i1s7bIZHIciW7j/s16yEYVYv KmcuPQW5ZoGK+TuDPIoME7lMpHxjJlP+GC8K6drjhuQ9/fyegrRb+xxUkQjdlB2MSstZ rEc6K2vc4kH4KnQO3bCOzVwClgXEr64sZmMmP/h6lFHeA0GPgI0Yfs7Van0ljOpdIW25 OWxBg/XIc3eHUkDNrx8QgMayuK3XsVPaBmTIzDxhtHrduPJVgGqwFKjRKdGN8qDe/u2F 5An7V9C3Y72rC/obRUtDqFBfZkouFgUZF6jKwWZipUFtzzgmMB8q9sxE+qUrsBAkudgI +RYA== X-Gm-Message-State: AOJu0YzsSb3dFmsUeP7JEheadVrvUW1eSn7irbEx2WwqmOnZQuO1SzQp GQMVDczd76Rmd5OuSL/yDTGtdueHkp+juU+JiL8kePaD X-Google-Smtp-Source: AGHT+IEq5ZIfgNoQLYwkVPNe+xAZKcwJ6Tz/vm5zVX5PlFOKgXK40kc79o/iKfVyndCWo2FAnVeWFYNHO14Rkw2u1T4= X-Received: by 2002:a05:6870:b91:b0:1e9:cddf:f825 with SMTP id lg17-20020a0568700b9100b001e9cddff825mr14901136oab.15.1698185822076; Tue, 24 Oct 2023 15:17:02 -0700 (PDT) MIME-Version: 1.0 From: Daniele Varrazzo Date: Wed, 25 Oct 2023 00:16:50 +0200 Message-ID: Subject: Improving `Connection.notifies()` in Psycopg 3.2 To: Psycopg Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, Psycopg 3 introduced a `Connection.notifies()` generator, yielding async notifications as they are received: for n in conn.notifies(): do_something_with(n) However it proved harder to use than expected: - during the loop, the connection cannot be used to run queries (and some would be pretty useful, such as LISTEN/UNLISTEN to change the channels to listen at); - it is not clear how to stop the generator. If assigned to a variable `gen`, it can be closed with `gen.close()` but it must be done from an external task/thread if the generator is waiting and I seem to remember that it's not possible to call close on an async generator from an external task. In https://github.com/psycopg/psycopg/pull/673 I propose to add optional parameters to the function: - `timeout`: max time to wait for notifications; - `stop_after`: stop after receiving this number of notifications (may return more than what requested if received in the same batch); I think these additions should make the generator easier to use, but I'm not completely sold on its interface. What do you think? Comments are welcome. Cheers -- Daniele