Обсуждение: NOTIFY/LISTEN why is not a callback as notice processing.

Поиск
Список
Период
Сортировка

NOTIFY/LISTEN why is not a callback as notice processing.

От
"Filonenko Michael"
Дата:
Hello.

I create simple mechanism to inform user about something in database
triggers. In my front-end I use PQsetNoticeReceiver, and display messages
in QTextEdit.
I think about multi-user environment. I read about NOTIFY/LISTEN, but find
no callback mechanism. Is it planning?


--
With best regards, Michael Filonenko.

Re: NOTIFY/LISTEN why is not a callback as notice processing.

От
Tom Lane
Дата:
"Filonenko Michael" <filonenko.mikhail@gmail.com> writes:
> I create simple mechanism to inform user about something in database
> triggers. In my front-end I use PQsetNoticeReceiver, and display messages
> in QTextEdit.
> I think about multi-user environment. I read about NOTIFY/LISTEN, but find
> no callback mechanism. Is it planning?

Not in libpq.  libpq is just a passive library, it can't cause actions
to happen when you aren't calling it.  So there's no point in a
callback: you might as well just test for occurrences of a NOTIFY at
times when you're prepared to handle it.

            regards, tom lane

Re: NOTIFY/LISTEN why is not a callback as notice processing.

От
"Daniel Verite"
Дата:
    Filonenko Michael wrote:

> I create simple mechanism to inform user about something in database
> triggers. In my front-end I use PQsetNoticeReceiver, and display messages
> in QTextEdit.
> I think about multi-user environment. I read about NOTIFY/LISTEN, but find
> no callback mechanism. Is it planning?

Not sure what the multi-user environment implies, but since you're using Qt,
you're probably interested in getting a Qt signal when a db notification
arrives. It turns out it's not hard to do: before calling LISTEN, instantiate
a QSocketNotifier object bound to the socket of the libpq connection, and
connect its activated() signal to a slot. In this slot, your code can deal
with the notification at the libpq level with PQconsumeInput() and
PQnotifies(), do your own processing, and just return.
With this method, you don't have any need for a callback or managing your own
loop: it is Qt's event loop that calls the slot, just as it does for
GUI-related events.

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org

Re: NOTIFY/LISTEN why is not a callback as notice processing.

От
Steve Atkins
Дата:
On Nov 10, 2010, at 4:30 PM, Daniel Verite wrote:

>     Filonenko Michael wrote:
>
>> I create simple mechanism to inform user about something in database
>> triggers. In my front-end I use PQsetNoticeReceiver, and display messages
>> in QTextEdit.
>> I think about multi-user environment. I read about NOTIFY/LISTEN, but find
>> no callback mechanism. Is it planning?
>
> Not sure what the multi-user environment implies, but since you're using Qt,
> you're probably interested in getting a Qt signal when a db notification
> arrives. It turns out it's not hard to do: before calling LISTEN, instantiate
> a QSocketNotifier object bound to the socket of the libpq connection, and
> connect its activated() signal to a slot. In this slot, your code can deal
> with the notification at the libpq level with PQconsumeInput() and
> PQnotifies(), do your own processing, and just return.
> With this method, you don't have any need for a callback or managing your own
> loop: it is Qt's event loop that calls the slot, just as it does for
> GUI-related events.

It's even easier than that - Qt supports listen/notify directly.

QSqlDriver::subscribeToNotification(), and connect the the notification()
signal.

Cheers,
  Steve


Re: NOTIFY/LISTEN why is not a callback as notice processing.

От
Vick Khera
Дата:
On Wed, Nov 10, 2010 at 5:20 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Not in libpq.  libpq is just a passive library, it can't cause actions
> to happen when you aren't calling it.  So there's no point in a
> callback: you might as well just test for occurrences of a NOTIFY at
> times when you're prepared to handle it.
>

On my systems where I use them more or less as "wakeup and do work"
signals, I just do a select() call on the connection socket.  When the
signal arrives, the socket becomes readable, and then I just query for
the notification.

Re: NOTIFY/LISTEN why is not a callback as notice processing.

От
Михаил Филоненко
Дата:

Thanks. But also I want to use postgresql and gtk with python or with c. I think, I need to make something like QSocketNotifier with callbacks instead of signals/slots. 

Well, why does libpq accumulate notifi and commit developer to create loop?

select(....)
PQconsumeInput(conn)
while ((notify = PQnotifies(conn)) != NULL)
// show notify

Interface for notice processing provided by libpq is simpler. Notice and notifies very 

receiver(connection, message) {// show notify
}
PQsetNoticeReceiver(receiver);



--
With best regards, Michael Filonenko