python async with psycopg2

From: Rita <rmorgan466(at)gmail(dot)com>
To: "pgsql-general(at)postgresql(dot)org >> PG-General Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: python async with psycopg2
Date: 2020-07-17 19:44:26
Message-ID: CAOF-Kfh1P9eanS0hecw_E9Awdsmn3b4uYpnV5SdL8Xz31U2rBw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am trying to listen to multiple channels thru pg_notify mechanism.

select pg_notify('mychan0',foo');
select pg_notify('mychan'1,'bar');

In python I have something like this

import select,time
import psycopg2
import psycopg2.extensions
from psycopg2.extras import wait_select

DSN="dbname=mydb user=user host=localhost"

def main():

conn = psycopg2.connect(DSN,async_=True)
wait_select(conn)
curs = conn.cursor()
curs.execute("LISTEN mychan0;")
#curs.execute("LISTEN mychan1;") #fails!
wait_select(conn)
while True:
wait_select(conn)
while conn.notifies:
print("Notify: %s"%conn.notifies.pop().payload)
time.sleep(1)
conn.close()

I keep getting

psycopg2.ProgrammingError: execute cannot be used while an asynchronous
query is underway

I prefer to stick with psycopg2 library instead of a wrapper.
What am I doing wrong?

--
--- Get your facts first, then you can distort them as you please.--

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daniele Varrazzo 2020-07-17 20:07:26 Re: python async with psycopg2
Previous Message Ailleen Pace 2020-07-17 19:08:09 DB Authentication with Label Security