如何在Sqlalchemy orm会话中正确禁用缓存? [英] How to disable caching correctly in Sqlalchemy orm session?
问题描述
我让我线程进入守护程序,该线程循环并执行以下查询:
I have I thread in a daemon, that loops and performs the following query:
try:
newsletter = self.session.query(models.Newsletter).\
filter(models.Newsletter.status == 'PROCESSING').\
limit(1).one()
except sa.orm.exc.NoResultFound:
self.logger.debug('No PROCESSING newsletters found. Sleeping...')
self.sleep()
return
# (...) more code to do with found newsletter
其中sleep方法只是停止执行该线程在配置的时间内运行,并且return语句返回到主循环。但是我发现,如果在守护程序运行时将任何新闻通讯的状态更改为处理中,则什么也不会发生。查询仍然引发NoResultFound。但是,如果重新启动守护程序,它将找到新闻通讯。因此,我知道该查询的结果必须缓存。我该怎么做才能使缓存无效? session.expire_all()不起作用。我还可以在每次迭代时创建新的Session()对象,但不知道这是否是一种有关系统资源的好方法。
Where the sleep method just stops the execution of this thread for the configured time and the return statement returns to the main loop. However I found, that if I change any newsletter's status to 'PROCESSING' while the daemon is running, nothing happens, ie. the query still raises NoResultFound. If I restart the daemon however, it will find the newsletter. So I see, that the results of this query must be cached. What can I do to invalidate the cache? session.expire_all() doesn't work. I could also create new Session() object every iteration, but don't know if it's a good approach regarding system resources.
推荐答案
SQLAlchemy 不会自行缓存。除非您明确实现了缓存,否则就是这样。
SQLAlchemy doesn't cache on itself. Unless you explicitly implemented a cache, like this one.
通过 echo = True
到您的 sessionmaker
并查看 日志记录
输出。
Pass echo=True
to your sessionmaker
and look into the logging
output.
这篇关于如何在Sqlalchemy orm会话中正确禁用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!