Improving `Connection.notifies()` in Psycopg 3.2

Поиск
Список
Период
Сортировка
От Daniele Varrazzo
Тема Improving `Connection.notifies()` in Psycopg 3.2
Дата
Msg-id CA+mi_8Z6pSgVOpWgretCKpop8MzEWnJ-R-A3c6KQdodYY4D0YQ@mail.gmail.com
обсуждение исходный текст
Список psycopg
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



В списке psycopg по дате отправления:

Предыдущее
От: Daniele Varrazzo
Дата:
Сообщение: Re: CPU usage for queries, psycopg 2 vs 3
Следующее
От: Ams Fwd
Дата:
Сообщение: 2-to-3 Question about adapter using AsIs