烧瓶g对象是否有超时?

时间:2019-01-09 09:30:25

标签: python flask python-huey

我将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对象是否超时或正在进行某种形式的垃圾回收?

1 个答案:

答案 0 :(得分:0)

只要请求仍然有效,g就会获得信息。

g绑定到application context,并将保留在内存中,直到请求结束。

绝对不是为任务队列存储用户信息的好地方