Re: NOTIFY/LISTEN in Postgresql

From: Sim Zacks <sim(at)compulab(dot)co(dot)il>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: NOTIFY/LISTEN in Postgresql
Date: 2012-10-15 14:09:01
Message-ID: 507C18FD.6010309@compulab.co.il
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<html style="direction: ltr;">
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<style type="text/css">body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
</head>
<body style="direction: ltr;"
bidimailui-detected-decoding-type="latin-charset" text="#000000"
bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 10/15/2012 02:54 PM, P. Broennimann
wrote:<br>
</div>
<blockquote
cite="mid:CACm4aU-eFNaDiYw9uH1n9VPgHN5SqseJ3HqsbgXd7-Cw++NF-g(at)mail(dot)gmail(dot)com"
type="cite"><font face="courier new, monospace">Hi there</font>
<div><font face="courier new, monospace"><br>
</font></div>
<div><font face="courier new, monospace">1) Can a Pg/SQL function
"listen" for a notification sent from an external instance?</font></div>
<div><span style="font-family:'courier new',monospace">I would
like my stored function to pause/wait and continue its
execution once an external event (NOTIFY event) occurs.</span></div>
</blockquote>
<blockquote
cite="mid:CACm4aU-eFNaDiYw9uH1n9VPgHN5SqseJ3HqsbgXd7-Cw++NF-g(at)mail(dot)gmail(dot)com"
type="cite"><br>
<div><font face="courier new, monospace">2) In&nbsp;</font><span
style="font-family:'courier new',monospace">Pg/SQL I can
implement a loop (until something happens) to pause/wait. This
costs CPU time -&gt; Is there another solution? Actually I
would need something like a trigger to give my stored function
the signal to continue.</span></div>
</blockquote>
<br>
Yes. I played around with this a while ago, and thought the
possibility was beyond cool.<br>
However, after playing around with it and getting some constructive
criticism, I decided that it would be better to keep the different
application layers completely separated, so that the database layer
would not be talking directly to the GUI.<br>
<br>
Here is an example function that I sent to the list a year+ ago.<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<a
href="http://archives.postgresql.org/pgsql-general/2011-06/msg00322.php">http://archives.postgresql.org/pgsql-general/2011-06/msg00322.php</a>
<br>
<br>
On the client end, I had something like this:<br>
class ListenThread(threading.Thread):&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; def __init__(self,frame):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threading.Thread.__init__(self)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.frame=frame<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; def run(self):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HOST, PORT = "192.168.1.207", 8080<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Create the server, binding to localhost on port 8080<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server = SocketServer.TCPServer((HOST, PORT),
MyTCPHandler)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server.frame=self.frame<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Activate the server; this will keep running until you<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # interrupt the program with Ctrl-C<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server.serve_forever()&nbsp;&nbsp; <br>
<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 3.6 KB

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2012-10-15 14:41:07 Re: NOTIFY/LISTEN in Postgresql
Previous Message Chris Travers 2012-10-15 13:49:43 Re: NOTIFY/LISTEN in Postgresql