Thread: Python + listen/notify

Python + listen/notify

From
Filipe Brandão
Date:
Hi all.
I needed to have a trigger firing after a commit, but allready realized it's not possible.
As i searched upon a solution i came across the LISTEN / NOTIFY. I haven't yet realized exactly what i can do with it,
butcan i use it to run a simple python script located out my DB? 
Can't run it inside the DB since i need to do it after the completion of a few triggers (after commit).
Any help would be much appreciated!
Thanks in advance!

Sent from my iPad

Re: Python + listen/notify

From
Josh Kupershmidt
Date:
On Tue, Jul 10, 2012 at 3:56 PM, Filipe Brandão <fg.brandao@gmail.com> wrote:
> Hi all.
> I needed to have a trigger firing after a commit, but allready realized it's not possible.
> As i searched upon a solution i came across the LISTEN / NOTIFY. I haven't yet realized exactly what i can do with
it,but can i use it to run a simple python script located out my DB? 
> Can't run it inside the DB since i need to do it after the completion of a few triggers (after commit).

Certainly: your python program should stay connected to the database,
waiting to receive the NOTIFYs, and then take appropriate action. See:
  http://packages.python.org/psycopg2/advanced.html#asynchronous-notifications

Josh

Re: Python + listen/notify

From
Josh Kupershmidt
Date:
[Please keep the list CC'd]

On Wed, Jul 11, 2012 at 3:58 AM, Filipe brandão <fg.brandao@gmail.com> wrote:

> Now, i only need to link these processes (trigger completion and running the
> script).
> How can i do it with Listen / Notify? Issue a notify in the end of the
> trigger and a listen on the python script?

Yes, that should work fine. See the doc pages for LISTEN and NOTIFY:
  http://www.postgresql.org/docs/current/static/sql-listen.html
  http://www.postgresql.org/docs/current/static/sql-notify.html

Along with the link I sent earlier to the psycopg2 section, assuming
you are in fact using psycopg2 as your database connector. I suggest
trying out the psycopg2 example in their docs verbatim first, then
adapting the example to your own code.

Re: Python + listen/notify

From
Filipe brandão
Date:
Hei Josh, problem solved!

Thanks for your help!

On Wed, Jul 11, 2012 at 4:27 PM, Josh Kupershmidt <schmiddy@gmail.com> wrote:
[Please keep the list CC'd]

On Wed, Jul 11, 2012 at 3:58 AM, Filipe brandão <fg.brandao@gmail.com> wrote:

> Now, i only need to link these processes (trigger completion and running the
> script).
> How can i do it with Listen / Notify? Issue a notify in the end of the
> trigger and a listen on the python script?

Yes, that should work fine. See the doc pages for LISTEN and NOTIFY:
  http://www.postgresql.org/docs/current/static/sql-listen.html
  http://www.postgresql.org/docs/current/static/sql-notify.html

Along with the link I sent earlier to the psycopg2 section, assuming
you are in fact using psycopg2 as your database connector. I suggest
trying out the psycopg2 example in their docs verbatim first, then
adapting the example to your own code.



--
Filipe Brandão