Skip to main content

轻松聆听和通知 PostgreSQL 通知系统

项目描述

pgnotify_u:更新了 pgnotify 模块。一个 python 库,可以通过 PostgreSQL 通知系统轻松监听和通知

例子

for用一个简单的循环来监听和处理 NOTIFY 事件,如下所示:

from pgnotify import await_pg_notifications

for notification in await_pg_notifications(
        'postgresql:///example',
        ['channel1', 'channel2']):

    print(notification.channel)
    print(notification.payload)

示例 2

通知:

from pgnotify import pg_notify
pg_notify("postgresql:///example", 'channel1', 'hello')

安装

可使用 python 包索引中的任何 python 包管理器安装,例如:

pip install pgnotify_u

所有的花里胡哨

您还可以处理超时和信号,如这个更完整的示例中所示:

import signal

from pgnotify import await_pg_notifications, get_dbapi_connection

# the first parameter of the await_pg_notifications
# loop is a dbapi connection in autocommit mode
CONNECT = "postgresql:///example"

# use this convenient method to create the right connection
# from a database URL
e = get_dbapi_connection(CONNECT)

SIGNALS_TO_HANDLE = [signal.SIGINT, signal.SIGTERM]

for n in await_pg_notifications(
    e,
    ["hello", "hello2"],
    timeout=10,
    yield_on_timeout=True,
    handle_signals=SIGNALS_TO_HANDLE,
):
    # the integer code of the signal is yielded on each
    # occurrence of a handled signal
    if isinstance(n, int):
        sig = signal.Signals(n)
        print(f"handling {sig.name}, stopping")
        break

    # the `yield_on_timeout` option makes the
    # loop yield `None` on timeout
    elif n is None:
        print("timeout, continuing")

    # handle the actual notify occurrences here
    else:
        print((n.pid, n.channel, n.payload))

进一步的文件来。

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

pgnotify_u-0.2.tar.gz (4.9 kB 查看哈希)

已上传 source

内置分布

pgnotify_u-0.2-py2.py3-none-any.whl (4.9 kB 查看哈希)

已上传 py2 py3