0

我每分钟都有一个程序从数据库(MySQL)查询数据。

while 1:
    self.alerteng.updateAndAnalyze()
    time.sleep(60)

但数据不会经常变化;可能一个小时或一天一次。(由另一个 C++ 程序更改)

我认为最好的方法是在发生更改时跟踪更改,然后查询并更新我的数据。

有什么建议吗?

4

2 回答 2

2

这取决于你在做什么,但 SQLAlchemy 的事件功能可能会帮助你。

它允许您在数据库中发生任何事情时运行代码,即在您插入新行或设置列值之后。我在 Flask 应用程序中使用它来启动通知或其他异步进程。

http://docs.sqlalchemy.org/en/rel_0_7/orm/events.html#mapper-events

这是来自 Flask 应用程序的玩具代码,每当在数据库中创建新的 YourModel 模型时,它将运行 kick_off_analysis() 函数。

from sqlalchemy import event

@event.listens_for(YourModel, "after_insert")
def kick_off_analysis(mapper, connection, your_model):
  # do stuff here

希望能帮助您入门。

于 2012-11-13T05:34:24.693 回答
0

I don't know how expensive updateAndAnalyze() is, but I'm pretty sure it's like most SQL commands: not something you really want to poll.

You have a textbook case for the Observer Pattern. You want MySQL to call something somehow in your code whenever it gets updated. I'm not positive of the exact mechanism to do this, but there should be way to set a trigger on your relevant tables where it can notify your code that the underlying table has been updated. Then, instead of polling, you basically get "interrupted" with knowledge that you need to do something. It will also eliminate that up-to-a-minute lag you're introducing, which will make whatever you're doing feel more snappy.

于 2012-11-11T15:28:53.867 回答