在PostgreSQL通知上执行函数
项目描述
PostgreSQL通知监听器
本项目是一个Python库,用于监听PostgreSQL数据库的通知。它提供了一种简单的方法,在数据库中发生特定事件时执行函数。
工作原理
监听器连接到PostgreSQL数据库并设置一个通知通道。然后,您可以将回调函数附加到该通道,当接收到通知时将执行这些函数。
安装
要安装库,请运行:pip install postgresql-notification-listener
使用方法
要使用此库,请按照以下步骤操作
- 在Python脚本中导入库:
from postgresql_notification_listener import NotificationListener
- 创建监听器实例
listener = NotificationListener("host=your_host port=your_port dbname=your_database user=your_username password=your_password")
- 定义一个当接收到通知时将被执行的回调函数。
- 使用
subscribe_to_channel
方法将您的回调函数附加到通知通道:listener.subscribe_to_channel("channel_to_listen", callback_function)
- 通过调用
start
方法开始监听通知:listener.start()
- 您可以通过
NOTIFY channel_to_listen
语句从PostgreSQL触发通知。 - 《start》方法会在被调用时一次性调用所有附加的回调函数。如果您不希望这种行为,请在start方法中传递
initial_run=False
参数:listener.start(initial_run=False)
- 您可以从监听实例的
last_notification
属性中获取导致回调的通知:listener.last_notification
API
- NotificationListener:此库的主要类。它负责设置通知通道和管理回调。
- subscribe_to_channel:将回调函数附加到特定通道。
subscribe_to_channel
方法需要两个必需参数:要监听的通道名称以及在接收到通知时执行的回调函数。 - start:开始监听通知。如果您不希望在调用时一次性调用所有附加的回调,请将
initial_run=False
作为参数传递。 - last_notification:返回导致回调的最新通知。
- subscribe_to_channel:将回调函数附加到特定通道。
项目详情
关闭
postgresql_notification_listener-2.0.1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 09b704ea68d8bdd7ee10ef109df587d020bd39677e9c69e8b8cfd1a32188c817 |
|
MD5 | 4aa64f000f32004be4782d19e3274492 |
|
BLAKE2b-256 | 252c98a228ef0a1e01410309d810b584658f883759d47add311b339b4b46dcff |
关闭
postgresql_notification_listener-2.0.1-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5161150375155d04ce7dcaaca32bb231196aa34878e7b2940bd6f7acef3e72c1 |
|
MD5 | 23bd7d20bb404f054373d97e46e83d83 |
|
BLAKE2b-256 | fc66146f499580c9c7afa9166faf3cd259f4bcbb6dbb183dca079678ce7c641b |