[英]python & postgresql: reliably check for updates in a specific table
Situation: I have a live trading script which computes all sorts of stuff every x minutes in my main thread
( Python ).情况:我有一个实时交易脚本,它在我的主
thread
( Python )中每 x 分钟计算一次各种内容。 the order sending is performed through such thread
.订单发送是通过这样的
thread
执行的。 the reception and execution of such orders though is a different matter as I cannot allow x minutes to pass but I need them as soon as they come in. I initialized another thread
to check for such data (execution) which is in a database table ( POSTGRES SQL
).尽管此类订单的接收和执行是另一回事,因为我不能让 x 分钟过去,但我一进来就需要它们。我初始化了另一个
thread
来检查数据库表中的此类数据(执行)( POSTGRES SQL
)。
Problem(s): I cannot continuosly perform query every xx ms, get data from DB, compare table length, and then get the difference for a variety of reasons (not only guy to use such DB, perforamnce issues, etc).问题:我不能连续每 xx 毫秒执行一次查询,从数据库中获取数据,比较表长度,然后由于各种原因(不仅是使用这种数据库的人,性能问题等)得到差异。 so I looked up some solutions and came up with this thread ( https://dba.stackexchange.com/questions/58214/getting-last-modification-date-of-a-postgresql-database-table ) where basically the gist of it was that "There is no reliable, authorative record of the last modified time of a table".
所以我查找了一些解决方案并提出了这个线程( https://dba.stackexchange.com/questions/58214/getting-last-modification-date-of-a-postgresql-database-table ),其中基本上是就是“没有可靠的、权威的表的最后修改时间记录”。
Question: what can I do about it, that is: getting near instantenuous responses from a postgres sql
table without overloading the whole thing using Python
?问题:我能做些什么,即:从
postgres sql
表获得近乎即时的响应,而无需使用Python
重载整个事情?
You can use notifications in postgresql:您可以在 postgresql 中使用通知:
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import select
def dblisten(dsn):
connection = psycopg2.connect(dsn)
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = connection.cursor()
cur.execute("LISTEN new_id;")
while True:
select.select([connection],[],[])
connection.poll()
events = []
while connection.notifies:
notify = connection.notifies.pop().payload
do_something(notify)
and install a trigger for each update:并为每个更新安装一个触发器:
CREATE OR REPLACE FUNCTION notify_id_trigger() RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('new_id', NEW.ID);
RETURN new;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER data_modified AFTER insert or update on data_table for each row execute procedure notify_id_trigger();")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.