我将Flask与Huey结合使用以在上下文之外处理长时间运行的任务。 因此,从本质上讲,烧瓶的上下文是由一个Huey使用者(一个工作进程)启动的,它需要花费很多时间才能完成很多工作。 在此过程中,我使用Flask全局'g'对象存储用户。
@staticmethod
def CurrentUser():
if g:
if hasattr(g, 'usr'):
user = g.usr
else:
# no user is linked to g yet - see if we can find and load one by searching for a UUID in the cookies
user = UserStateManager.LoadUserState()
g.usr = user
else:
# no g object - maybe we are running outside of the flask context - use a default user
user = UserStateManager.DefaultUserstate()
return user
问题是20分钟左右后,g.usr对象突然消失了。这是一个很难调试的问题,因为此时的代码是由Huey任务工作者运行的,而我不能在IDE中运行它。 (嗯,我可以,但是上下文不同,所以情况就不同了。)
我可以看到它很好用,因为它会将包含用户ID的记录写入数据库,但是大约20分钟后,用户ID字段突然变空。看起来好像g对象在某个地方被清理了。
所以我的问题是,是否有人知道什么可能触发g对象中的usr数据崩溃。 g对象是否超时或正在进行某种形式的垃圾回收?