在烧瓶应用程序中保持全局状态 [英] Preserving global state in a flask application

查看:227
本文介绍了在烧瓶应用程序中保持全局状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



据我了解, ,应用程序上下文,特别是 flask.g对象


$ b

设置:

import flask as f

app = f.Flask(__ name__)

现在,如果我做了

 fgfoo =bar
print fgfoo
with app.app_context p $ p
$ b

打印 bar



继续():
print fgfoo



AttributeError:'_AppCtxGlobals'对象没有属性'foo'

I不理解它,文档根本没有帮助。如果我正确地读取它们,状态应该已经被保留。



我的另一个想法是简单地使用模块范围的变量:

cache = {}
$ b $ def some_function():
cache ['foo'] =bar

但是好像每个请求都会重置。 / p>

如何正确执行此操作?

编辑: Flask 10.1

解决方案

基于你的问题,我认为你对全局的定义感到困惑。

在一个库存Flask安装程序中,您有一个Flask服务器,它有多个线程和潜在的多个进程处理请求。假设你有一个像itemlist = []这样的股票全局变量,并且你想要在每一个请求中都加入它 - 比如每次有人向一个端点发出一个POST请求。这在理论和实践上是完全可能的。这也是一个非常糟糕的主意。



问题是,你不能轻易地控制哪些线程和进程胜利 - 列表可能以一个非常顺利的顺序,或完全损坏。所以现在你需要谈论锁,互斥和其他原语。这是很难和烦人的。



您应该保持Web服务器本身尽可能无状态。每个请求应该完全独立,不共享服务器中的任何状态。相反,使用数据库或缓存层将为您处理状态。这似乎更复杂,但实际上更简单。以SQLite为例,这非常简单。



为了解决'flask.g'对象,这是一个基于每个请求的全局对象。

http://flask.pocoo.org/ docs / api /#flask.g



它在请求之间被抹去干净,不能用来共享它们之间的状态。 $ b

I am trying to save a cache dictionary in my flask application.

As far as I understand it, the Application Context, in particualr the flask.g object should be used for this.

Setup:

import flask as f

app = f.Flask(__name__)

Now if I do

with app.app_context():
    f.g.foo = "bar"
    print f.g.foo

it prints bar.

Continuing with the following

with app.app_context():
    print f.g.foo

AttributeError: '_AppCtxGlobals' object has no attribute 'foo'

I don’t understand it and the docs are not helping at all. If I read them correctly the state should have been preserved.

Another idea I had was to simply use module-wide variables:

cache = {}

def some_function():
    cache['foo'] = "bar"

but it seems like these get reset with every request.

How to do this correctly?

Edit: Flask 10.1

解决方案

Based on your question, I think you're confused about the definition of "global".

In a stock Flask setup, you have a Flask server with multiple threads and potentially multiple processes handling requests. Suppose you had a stock global variable like "itemlist = []", and you wanted to keep adding to it in every request - say, every time someone made a POST request to an endpoint. This is totally possible in theory and practice. It's also a really bad idea.

The problem is that you can't easily control which threads and processes "win" - the list could up in a really wonky order, or get corrupted entirely. So now you need to talk about locks, mutexs, and other primitives. This is hard and annoying.

You should keep the webserver itself as stateless as possible. Each request should be totally independent and not share any state in the server. Instead, use a database or caching layer which will handle the state for you. This seems more complicated but is actually simpler in practice. Check out SQLite for example ; it's pretty simple.

To address the 'flask.g' object, that is a global object on a per request basis.

http://flask.pocoo.org/docs/api/#flask.g

It's "wiped clean" between requests and cannot be used to share state between them.

这篇关于在烧瓶应用程序中保持全局状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆