X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 33D763A3D22 for ; Tue, 4 Jan 2005 03:25:40 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 49020-02 for ; Tue, 4 Jan 2005 03:25:31 +0000 (GMT) Received: from hub.org (hub.org [200.46.204.220]) by svr1.postgresql.org (Postfix) with ESMTP id 8BE183A3D33 for ; Tue, 4 Jan 2005 03:25:34 +0000 (GMT) Received: from localhost (unknown [200.46.204.144]) by hub.org (Postfix) with ESMTP id 777FD12970A for ; Mon, 3 Jan 2005 23:25:30 -0400 (AST) Received: from hub.org ([200.46.204.220]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 46652-08 for ; Tue, 4 Jan 2005 03:25:19 +0000 (GMT) Received: from ganymede.hub.org (blk-222-250-144.eastlink.ca [24.222.250.144]) by hub.org (Postfix) with ESMTP id 5C95012925B for ; Mon, 3 Jan 2005 23:25:18 -0400 (AST) Received: by ganymede.hub.org (Postfix, from userid 1000) id 9F16B4992F; Mon, 3 Jan 2005 23:25:24 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by ganymede.hub.org (Postfix) with ESMTP id 99ABA4989D for ; Mon, 3 Jan 2005 23:25:24 -0400 (AST) X-Return-Path: X-Received: from localhost ([unix socket]) by ganymede.hub.org (Cyrus v2.2.10) with LMTPA; Mon, 03 Jan 2005 23:21:35 -0400 X-Sieve: CMU Sieve 2.2 X-Received: from localhost (localhost [127.0.0.1]) by ganymede.hub.org (Postfix) with ESMTP id 4B9A248553 for ; Mon, 3 Jan 2005 23:20:38 -0400 (AST) X-Received: from mail.postgresql.org [200.46.204.71] by localhost with IMAP (fetchmail-6.2.5) for scrappy@localhost (single-drop); Mon, 03 Jan 2005 23:20:38 -0400 (AST) X-Received: from svr1.postgresql.org ([unix socket]) by svr1.postgresql.org (Cyrus v2.2.10) with LMTPA; Tue, 04 Jan 2005 03:20:22 +0000 X-Sieve: CMU Sieve 2.2 X-Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A2D963A3D2F for ; Tue, 4 Jan 2005 03:20:22 +0000 (GMT) X-Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 46385-06 for ; Tue, 4 Jan 2005 03:20:14 +0000 (GMT) X-Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id D32733A3C84 for ; Tue, 4 Jan 2005 03:20:16 +0000 (GMT) X-Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id j043Jgsf039387 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 3 Jan 2005 20:19:45 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) X-Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id j043JggN080779; Mon, 3 Jan 2005 20:19:42 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) X-Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id j043Jbvu080777; Mon, 3 Jan 2005 20:19:37 -0700 (MST) (envelope-from mfuhr) Date: Mon, 3 Jan 2005 20:19:37 -0700 From: Michael Fuhr To: Thierry Missimilly Cc: postgresql Subject: Re: [Fwd: How to use LISTEN / NOTIFY in a Perl program] Message-ID: <20050104031937.GA80695@winnie.fuhr.org> References: <41D905A8.9020002@bull.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <41D905A8.9020002@bull.net> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-DCC: : X-Spam-Pyzor: ReSent-Date: Mon, 3 Jan 2005 23:25:19 -0400 (AST) Resent-From: "Marc G. Fournier" Resent-To: pgsql-general@postgresql.org ReSent-Subject: Re: [GENERAL] [Fwd: How to use LISTEN / NOTIFY in a Perl program] ReSent-Message-ID: <20050103232519.R46973@ganymede.hub.org> X-Virus-Scanned: by amavisd-new at hub.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.946 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200501/107 X-Sequence-Number: 71056 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/