# pgnotify_u: Updated pgnotify module. A python library to easily LISTEN and NOTIFY with PostgreSQL notifications system
## Example
LISTEN to and process NOTIFY events with a simple `for` loop, like so:
```python
from pgnotify import await_pg_notifications
for notification in await_pg_notifications(
'postgresql:///example',
['channel1', 'channel2']):
print(notification.channel)
print(notification.payload)
```
## Example2
NOTIFY to:
```python
from pgnotify import pg_notify
pg_notify("postgresql:///example", 'channel1', 'hello')
```
## Install
Installable with any python package manager from the python package index, eg:
```shell
pip install pgnotify_u
```
## All the bells and whistles
You can also handle timeouts and signals, as in this more fully-fleshed example:
```python
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))
```
Further documentation to come.
Python库 | pgnotify_u-0.2.tar.gz
版权申诉
66 浏览量
2022-04-12
18:09:42
上传
评论
收藏 5KB GZ 举报
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
最新资源
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈