diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-11-06 20:59:10 +0000 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-11-08 01:28:00 +0000 |
| commit | 2f582da1f06048695839b503e30b9285c443503a (patch) | |
| tree | 17ce5e6df195afa4f7391fef2a85fc5a3be700d2 /doc/src | |
| parent | 7276c4a6b1512a30cf1f4abe7d15381da2b08ffb (diff) | |
| download | psycopg2-2f582da1f06048695839b503e30b9285c443503a.tar.gz | |
Notifcation example improved.
Diffstat (limited to 'doc/src')
| -rw-r--r-- | doc/src/advanced.rst | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/doc/src/advanced.rst b/doc/src/advanced.rst index f6aeede..12eb159 100644 --- a/doc/src/advanced.rst +++ b/doc/src/advanced.rst @@ -208,10 +208,6 @@ read: 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 facilities offered by PostgreSQL commands |LISTEN|_ and |NOTIFY|_. Please 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 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. Because of the way sessions interact with notifications (see |NOTIFY|_ @@ -253,26 +249,34 @@ something to read:: curs = conn.cursor() curs.execute("LISTEN test;") - # Payload only available since PostgreSQL 9.0 - print "Waiting for 'NOTIFY test', 'hello'" + print "Waiting for notifications on channel 'test'" while 1: if select.select([conn],[],[],5) == ([],[],[]): print "Timeout" else: conn.poll() 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 -:program:`psql` shell, the output may look similar to:: +Running the script and executing a command such as :sql:`NOTIFY test, 'hello'` +in a separate :program:`psql` shell, the output may look similar to:: - Waiting for 'NOTIFY test' + Waiting for notifications on channel 'test' Timeout Timeout - Got NOTIFY: Notify(6535, 'test', 'hello') + Got NOTIFY: 6535 test hello 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:: |
