Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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...

Comment on AnyEvent and Postgres Listen/Notify
Download Code
Re: AnyEvent and Postgres Listen/Notify
by nikosv (Hermit) 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

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.

Log In?
Username:
Password:

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
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2015-07-05 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls