如何接收关于表格更改的自动通知? [英] How to receive automatic notifications about changes in tables?

查看:234
本文介绍了如何接收关于表格更改的自动通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 PostgreSQL Python 。我正在使用Python访问PostgreSQL

What I am using: PostgreSQL and Python. I am using Python to access PostgreSQL

我需要:在Python上接收自动通知,如果有人在数据库上的特定表上记录一些东西。

What I need: Receive a automatic notification, on Python, if anyone records something on a specific table on database.

我认为可以使用一个例程去到该表,一段时间间隔,并检查更改。但是,需要一个循环,我想要一个类似于异步方式的

I think that it is possible using a routine that go to that table, over some interval, and check changes. But it requires a loop and I would like something like an a assynchronous way.

可以吗? / p>

Is it possible?

推荐答案

donmage是对的 - LISTEN NOTIFY 是你想要的您仍然需要一个轮询循环,但它非常轻便,并且不会导致可检测的服务器负载。

donmage is quite right - LISTEN and NOTIFY are what you want. You'll still need a polling loop, but it's very lightweight, and won't cause detectable server load.

如果您想要 psycopg2 可以在您的程序中随时触发回调,您可以通过生成一个线程并让该线程执行轮询循环来实现。检查psycopg2是否强制线程安全连接访问;如果没有,则需要自己进行锁定,以便您的轮询循环仅在连接空闲时运行,并且没有其他查询会中断轮询周期。或者您可以使用第二个连接进行事件轮询。

If you want psycopg2 to trigger callbacks at any time in your program, you can do this by spawning a thread and having that thread execute the polling loop. Check to see whether psycopg2 enforces thread-safe connection access; if it doesn't, you'll need to do your own locking so that your polling loop only runs when the connection is idle, and no other queries interrupt a polling cycle. Or you can just use a second connection for your event polling.

无论哪种方式,当轮询通知事件的后台线程接收到一个时,它可以调用一个Python回调函数由主程序提供,可能会修改程序其余部分共享的数据结构/变量。小心,如果你这样做,它可以很快成为一个噩梦维护。

Either way, when the background thread that's polling for notify events receives one, it can invoke a Python callback function supplied by your main program, which might modify data structures / variables shared by the rest of the program. Beware, if you do this, that it can quickly become a nightmare to maintain.

如果你采取这种方法,我强烈建议使用多线程 / 多处理模块。他们将使您的生活变得更加轻松,提供简单的方法来在线程之间交换数据,并将聆听线程的修改限制在简单易控的位置。

If you take that approach, I strongly suggest using the multithreading / multiprocessing modules. They will make your life massively easier, providing simple ways to exchange data between threads, and limiting modifications made by the listening thread to simple and well-controlled locations.

如果使用线程而不是进程,重要的是要明白,在cPython(即普通Python)中,您不能有真正的回调中断,因为一次只能在cPython中执行一个线程。了解更多关于全球口译锁(GIL)的信息。由于这个限制(而且默认情况下并发共享的更简单,更安全的性质),我经常喜欢多处理到多线程。

If using threads instead of processes, it is important to understand that in cPython (i.e. "normal Python") you can't have a true callback interrupt, because only one thread may be executing in cPython at once. Read about the "global interpreter lock" (GIL) to understand more about this. Because of this limitation (and the easier, safer nature of shared-nothing by default concurrency) I often prefer multiprocessing to multithreading.

这篇关于如何接收关于表格更改的自动通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆