mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-26 02:43:43 +03:00
Notifcation example improved.
This commit is contained in:
parent
7276c4a6b1
commit
2f582da1f0
|
@ -208,10 +208,6 @@ read:
|
||||||
Asynchronous notifications
|
Asynchronous notifications
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
.. versionchanged:: 2.3
|
|
||||||
Added `~psycopg2.extensions.Notify` object allowing to retrieve
|
|
||||||
the notification payload if connected to a PostgreSQL 9.0 server.
|
|
||||||
|
|
||||||
Psycopg allows asynchronous interaction with other database sessions using the
|
Psycopg allows asynchronous interaction with other database sessions using the
|
||||||
facilities offered by PostgreSQL commands |LISTEN|_ and |NOTIFY|_. Please
|
facilities offered by PostgreSQL commands |LISTEN|_ and |NOTIFY|_. Please
|
||||||
refer to the PostgreSQL documentation for examples about how to use this form of
|
refer to the PostgreSQL documentation for examples about how to use this form of
|
||||||
|
@ -219,7 +215,7 @@ communication.
|
||||||
|
|
||||||
Notifications are instances of the `~psycopg2.extensions.Notify` object made
|
Notifications are instances of the `~psycopg2.extensions.Notify` object made
|
||||||
available upon reception in the `connection.notifies` list. Notifications can
|
available upon reception in the `connection.notifies` list. Notifications can
|
||||||
be sent from Python code simply using a :sql:`NOTIFY` command in an
|
be sent from Python code simply executing a :sql:`NOTIFY` command in an
|
||||||
`~cursor.execute()` call.
|
`~cursor.execute()` call.
|
||||||
|
|
||||||
Because of the way sessions interact with notifications (see |NOTIFY|_
|
Because of the way sessions interact with notifications (see |NOTIFY|_
|
||||||
|
@ -253,26 +249,34 @@ something to read::
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute("LISTEN test;")
|
curs.execute("LISTEN test;")
|
||||||
|
|
||||||
# Payload only available since PostgreSQL 9.0
|
print "Waiting for notifications on channel 'test'"
|
||||||
print "Waiting for 'NOTIFY test', 'hello'"
|
|
||||||
while 1:
|
while 1:
|
||||||
if select.select([conn],[],[],5) == ([],[],[]):
|
if select.select([conn],[],[],5) == ([],[],[]):
|
||||||
print "Timeout"
|
print "Timeout"
|
||||||
else:
|
else:
|
||||||
conn.poll()
|
conn.poll()
|
||||||
while conn.notifies:
|
while conn.notifies:
|
||||||
print "Got NOTIFY:", conn.notifies.pop()
|
notify = conn.notifies.pop()
|
||||||
|
print "Got NOTIFY:", notify.pid, notify.channel, notify.payload
|
||||||
|
|
||||||
Running the script and executing the command :sql:`NOTIFY test` in a separate
|
Running the script and executing a command such as :sql:`NOTIFY test, 'hello'`
|
||||||
:program:`psql` shell, the output may look similar to::
|
in a separate :program:`psql` shell, the output may look similar to::
|
||||||
|
|
||||||
Waiting for 'NOTIFY test'
|
Waiting for notifications on channel 'test'
|
||||||
Timeout
|
Timeout
|
||||||
Timeout
|
Timeout
|
||||||
Got NOTIFY: Notify(6535, 'test', 'hello')
|
Got NOTIFY: 6535 test hello
|
||||||
Timeout
|
Timeout
|
||||||
...
|
...
|
||||||
|
|
||||||
|
Notice that the payload is only available from PostgreSQL 9.0: notifications
|
||||||
|
received from a previous version server will have the `!payload` attribute set
|
||||||
|
to the empty string.
|
||||||
|
|
||||||
|
.. versionchanged:: 2.3
|
||||||
|
Added `~psycopg2.extensions.Notify` object and handling notification
|
||||||
|
payload.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
|
|
Loading…
Reference in New Issue
Block a user