Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

AnyEvent and Postgres Listen/Notify

by xtpu2 (Acolyte)
on Apr 12, 2014 at 20:13 UTC ( #1082121=perlquestion: print w/replies, xml ) Need Help??
xtpu2 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to make use of the Postgres Listen/Notify feature in my AnyEvent-based program. I tried looking for a ready solution and found AnyEvent::Pg on CPAN, but I've not been able to get the module to work (and the developer does mention that it is in very early development). So I was trying to figure out the best way to set this up with the tools I have.

Listen/Notify is supported by DBD:Pg, so the solution I came up with is to poll $dbh to see if a notification has been received, like so (posting only the relevant code):

my $w = AnyEvent->timer ( after => 0, interval => 2, cb => sub { if (my $notify = $dbh->pg_notifies){ # Do something } } );

This works. However, I'm not sure this is the best way to do it because I'm just not that familiar with event-based programming. I would really appreciate alternate solutions / thoughts from more knowledgeable people...

Replies are listed 'Best First'.
Re: AnyEvent and Postgres Listen/Notify
by salva (Abbot) on Apr 14, 2014 at 07:06 UTC
    Hi, I am the author of AnyEvent::Pg.

    I could help you to get the module running if you describe your issues. The module is still pretty young and in most cases DBI/DBD::Pg is the preferred choice, but I use the LISTEN/NOTIFY feature myself and it works well, at least on Linux systems.

Re: AnyEvent and Postgres Listen/Notify
by nikosv (Chaplain) on Apr 14, 2014 at 06:57 UTC
    You could use the inherent DBMS's database events. You make an application, preferably set up as a service for constant monitoring, that registers and listens for the event. As soon as the event is raised the service gets the event details (name,text) and acts upon it.

    For a good example on it check the event.t source of DBD-IngresII. Might be Ingres specific and not Postgres but it illustrates the point clearly

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1082121]
Approved by Old_Gray_Bear
Front-paged by mtmcc
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2016-10-28 03:02 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (374 votes). Check out past polls.