This package makes it simple to use PostGreSQL’s NOTIFY/LISTEN eventing system from Python in a consistent, pleasing manner.
Note that this project officially supports Python 3.6+. This is primarily due to static typing.
Example
The following shows an example of the package in action.
Assumptions
PostGreSQL server is running locally.
default database (postgres) is available.
table exists in database in the public schema with the name orders.
from psycopg2 import connect
from psycopg2_pgevents.trigger import install_trigger, \
install_trigger_function, uninstall_trigger, uninstall_trigger_function
from psycopg2_pgevents.event import poll, register_event_channel, \
unregister_event_channel
connection = connect(dsn='postgres:///postgres')
connection.autocommit = True
install_trigger_function(connection)
install_trigger(connection, 'orders')
register_event_channel(connection)
try:
print('Listening for events...')
while True:
for evt in poll(connection):
print('New Event: {}'.format(evt))
except KeyboardInterrupt:
print('User exit via Ctrl-C; Shutting down...')
unregister_event_channel(connection)
uninstall_trigger(connection, 'orders')
uninstall_trigger_function(connection)
print('Shutdown complete.')
Troubleshooting
The connection’s autocommit property must be enabled for this package to operate correctly. This requirement is provided by PostGreSQL’s NOTIFY/LISTEN mechanism.
The same connection that is used with register_event_channel() must be used with poll() in order to receive events. This is due to the nature of how PostGreSQL manages “listening” connections.
If the table that you’d like to listen to is not in the public schema, the schema name must be given as a keyword argument in the install_trigger() method.
Metadata
Release files for psycopg2-pgevents 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| psycopg2-pgevents-0.2.2.tar.gz | 9.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| psycopg2_pgevents-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.9 kB
Release files / psycopg2-pgevents-0.2.2.tar.gz
| Download URL | psycopg2-pgevents-0.2.2.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d165f8cd64543b7c03013f5ef56acbb1409a93dbd255687fae977050db0f6419
|
|
BLAKE2b-256 checksum How to use checksums |
78d33c72d9b8d1343f9b676df87288e510191e433f4c0c54cf77e59e46447c01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.5 CPython/3.7.10 Linux/5.4.0-1043-azure
|
Release files / psycopg2_pgevents-0.2.2-py3-none-any.whl
| Download URL | psycopg2_pgevents-0.2.2-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
48b3440d28fe42506786c3772d09205bb4cd9b4537f547f67b3e490a73163c23
|
|
BLAKE2b-256 checksum How to use checksums |
292724b8e1e7655c988c2232a087e0d3c79edbba99edafe4882629daaf5afcd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.5 CPython/3.7.10 Linux/5.4.0-1043-azure
|