Re: [Fwd: How to use LISTEN / NOTIFY in a Perl program]

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Thierry Missimilly <Thierry(dot)Missimilly(at)bull(dot)net>
Cc: postgresql <psql-general(at)postgresql(dot)org>
Subject: Re: [Fwd: How to use LISTEN / NOTIFY in a Perl program]
Date: 2005-01-04 03:19:37
Message-ID: 20050104031937.GA80695@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Jan 03, 2005 at 09:43:20AM +0100, Thierry Missimilly wrote:
>
> I have spend some time to read the very interesting feature LISTEN /
> NOTIFY. I have found a lot of documentation on how to use in psql but
> nothing on how to use in Perl DBI program. I don't know if it works.
> I'll be very happy if someone had used it in a Perl script and send me
> an example.

Here's a simple example. Set $dbsource, $dbuser, and $dbpass to
appropriate values and start the script, then open a separate
connection to the database and execute "NOTIFY foo". Each time you
do that, the script should print that it received a notification.

#!/usr/bin/perl -T

use strict;
use warnings;
use DBI;
use IO::Select;

$| = 1;

my $dbsource = "dbi:Pg:dbname=test";
my $dbuser = "testuser";
my $dbpass = "testpassword";
my $dbattr = {RaiseError => 1, AutoCommit => 1};

my $dbh = DBI->connect($dbsource, $dbuser, $dbpass, $dbattr);

$dbh->do("LISTEN foo");

my $fd = $dbh->func("getfd");
my $sel = IO::Select->new($fd);

while (1) {
print "waiting...";
$sel->can_read;
my $notify = $dbh->func("pg_notifies");
if ($notify) {
my ($relname, $pid) = @$notify;
my $row = $dbh->selectrow_hashref("SELECT now()");
print "$relname from PID $pid at $row->{now}\n";
}
}

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2005-01-04 04:17:40 Re: Roadmap for Database Kernel XA Support
Previous Message Martijn van Oosterhout 2005-01-03 23:15:49 Re: Postgresql website issues.