繁体   English   中英

如何使用PyGreSQL接收通知?

[英]How to receive notices with PyGreSQL?

我在Postgres 9.5中使用PyGreSQL 4.1.1,并编写了一些存储函数。 我在函数内部使用了具有不同级别的RAISE进行调试,这在psql效果很好,但是我还没有找到在Python中访问这些消息的方法。

例:

CREATE OR REPLACE FUNCTION my_function()  RETURNS BOOLEAN AS $_$
    BEGIN
        RAISE NOTICE 'A notice from my function.';
        RETURN TRUE;
    END
$_$ LANGUAGE plpgsql;

我的Python代码如下所示:

conn = pgdb.connect(database = 'mydb', user = 'myself')
cursor = conn.cursor()
cursor.execute("SELECT my_function()"):

运行my_function()之后如何访问通知( A notice from my function. my_function()

由于@klin的评论,我发现了一个不太干净的解决方案。 pgdb.Connection对象存储底层pg.Connection在一个名为私有财产的对象_cnx 因此,您可以这样设置通知接收者:

def my_notice_receiver(notice):
    logging.info("Notice: %s", notice)

conn._cnx.set_notice_receiver(my_notice_receiver)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM