Python 全局变量在烧瓶中线程安全吗?如何在请求之间共享数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32815451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:19:42  来源:igfitidea点击:

Are global variables thread safe in flask? How do I share data between requests?

pythonflaskthread-safety

提问by sayantankhan

In my app the state of a common object is changed by making requests, and the response depends on the state.

在我的应用程序中,通过发出请求来更改公共对象的状态,并且响应取决于状态。

class SomeObj():
    def __init__(self, param):
        self.param = param
    def query(self):
        self.param += 1
        return self.param

global_obj = SomeObj(0)

@app.route('/')
def home():
    flash(global_obj.query())
    render_template('index.html')

If I run this on my development server, I expect to get 1, 2, 3 and so on. If requests are made from 100 different clients simultaneously, can something go wrong? The expected result would be that the 100 different clients each see a unique number from 1 to 100. Or will something like this happen:

如果我在我的开发服务器上运行它,我希望得到 1、2、3 等等。如果同时从 100 个不同的客户端发出请求,是否会出现问题?预期的结果是 100 个不同的客户端每个看到一个从 1 到 100 的唯一数字。 或者会发生这样的事情:

  1. Client 1 queries. self.paramis incremented by 1.
  2. Before the return statement can be executed, the thread switches over to client 2. self.paramis incremented again.
  3. The thread switches back to client 1, and the client is returned the number 2, say.
  4. Now the thread moves to client 2 and returns him/her the number 3.
  1. 客户端 1 查询。self.param增加 1。
  2. 在可以执行 return 语句之前,线程切换到客户端 2。self.param再次递增。
  3. 线程切换回客户端 1,并且客户端返回数字 2,比如说。
  4. 现在线程移动到客户端 2 并向他/她返回数字 3。

Since there were only two clients, the expected results were 1 and 2, not 2 and 3. A number was skipped.

由于只有两个客户端,因此预期结果是 1 和 2,而不是 2 和 3。跳过了一个数字。

Will this actually happen as I scale up my application? What alternatives to a global variable should I look at?

当我扩展我的应用程序时,这真的会发生吗?我应该查看全局变量的哪些替代方案?

采纳答案by davidism

You can't use global variables to hold this sort of data. Not only is it not thread safe, it's not processsafe, and WSGI servers in production spawn multiple processes. Not only would your counts be wrong if you were using threads to handle requests, they would also vary depending on which process handled the request.

您不能使用全局变量来保存此类数据。它不仅不是线程安全的,也不是进程安全的,而且生产环境中的 WSGI 服务器会产生多个进程。如果您使用线程来处理请求,您的计数不仅会出错,而且还会因处理请求的进程而异。

Use a data source outside of Flask to hold global data. A database, memcached, or redis are all appropriate separate storage areas, depending on your needs. If you need to load and access Python data, consider multiprocessing.Manager. You could also use the session for simple data that is per-user.

使用 Flask 之外的数据源来保存全局数据。根据您的需要,数据库、memcached 或 redis 都是合适的独立存储区域。如果您需要加载和访问 Python 数据,请考虑multiprocessing.Manager. 您还可以将会话用于每个用户的简单数据。



The development server may run in single thread and process. You won't see the behavior you describe since each request will be handled synchronously. Enable threads or processes and you will see it. app.run(threaded=True)or app.run(processes=10). (In 1.0 the server is threaded by default.)

开发服务器可以在单线程和进程中运行。您不会看到您描述的行为,因为每个请求都将被同步处理。启用线程或进程,您将看到它。app.run(threaded=True)app.run(processes=10)。(在 1.0 中,服务器默认是线程化的。)



Some WSGI servers may support gevent or another async worker. Global variables are still not thread safe because there's still no protection against most race conditions. You can still have a scenario where one worker gets a value, yields, another modifies it, yields, then the first worker also modifies it.

一些 WSGI 服务器可能支持 gevent 或其他异步工作者。全局变量仍然不是线程安全的,因为仍然没有针对大多数竞争条件的保护。你仍然可以有一个场景,一个工人得到一个值,产生,另一个修改它,产生,然后第一个工人也修改它。



If you need to store some global data duringa request, you may use Flask's gobject. Another common case is some top-level object that manages database connections. The distinction for this type of "global" is that it's unique to each request, not used betweenrequests, and there's something managing the set up and teardown of the resource.

如果您需要请求期间存储一些全局数据,您可以使用 Flask 的gobject。另一种常见情况是一些管理数据库连接的顶级对象。这种类型的“全局”的区别在于它对每个请求都是唯一的,而不是请求之间使用,并且有一些东西可以管理资源的设置和拆卸。

回答by lhk

This is not really an answer to thread safety of globals.

这并不是对全局线程安全的真正答案。

But I think it is important to mention sessions here. You are looking for a way to store client-specific data. Every connection should have access to its own pool of data, in a threadsafe way.

但我认为在这里提及会议很重要。您正在寻找一种存储客户端特定数据的方法。每个连接都应该以线程安全的方式访问自己的数据池。

This is possible with server-side sessions, and they are available in a very neat flask plugin: https://pythonhosted.org/Flask-Session/

这在服务器端会话中是可能的,它们在一个非常简洁的烧瓶插件中可用:https: //pythonhosted.org/Flask-Session/

If you set up sessions, a sessionvariable is available in all your routes and it behaves like a dictionary. The data stored in this dictionary is individual for each connecting client.

如果你设置了会话,一个session变量在你的所有路由中都可用,它的行为就像一个字典。存储在此字典中的数据对于每个连接的客户端都是单独的。

Here is a short demo:

这是一个简短的演示:

from flask import Flask, session
from flask_session import Session

app = Flask(__name__)
# Check Configuration section for more details
SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
Session(app)

@app.route('/')
def reset():
    session["counter"]=0

    return "counter was reset"

@app.route('/inc')
def routeA():
    if not "counter" in session:
        session["counter"]=0

    session["counter"]+=1

    return "counter is {}".format(session["counter"])

@app.route('/dec')
def routeB():
    if not "counter" in session:
        session["counter"] = 0

    session["counter"] -= 1

    return "counter is {}".format(session["counter"])


if __name__ == '__main__':
    app.run()

After pip install Flask-Session, you should be able to run this. Try accessing it from different browsers, you'll see that the counter is not shared between them.

之后pip install Flask-Session,您应该能够运行它。尝试从不同的浏览器访问它,您会看到计数器没有在它们之间共享。

回答by R. Simac

While totally accepting the above upvoted answers, and discouraging use of globals for production and scalable flask storage, for the purpose of prototyping or really simple servers, running under the flask 'development server'...

虽然完全接受上述赞成的答案,并且不鼓励将全局变量用于生产和可扩展的烧瓶存储,但出于原型设计或真正简单的服务器的目的,在烧瓶“开发服务器”下运行......

... the python built-in data types, and I personally used and tested the global dict, as per python docs (https://docs.python.org/3/glossary.html#term-global-interpreter-lock) are threadsafe. Not processsafe.

... python 内置数据类型,我个人使用并测试了 global dict,根据 python 文档(https://docs.python.org/3/glossary.html#term-global-interpreter-lock)是线程安全。不处理安全。

The insertions, lookups, and reads from such (server global) dict will be ok from each (possibly concurrent) flask session running under development server.

对于在开发服务器下运行的每个(可能是并发的)flask 会话,从此类(服务器全局)dict 进行的插入、查找和读取都可以。

When such global dict is keyed with unique flask session key, it can be rather useful for server side storage of session specific data otherwise not fitting into the cookie (max size 4k).

当这样的全局字典使用唯一的flask 会话密钥作为键时,它对于会话特定数据的服务器端存储非常有用,否则不适合cookie(最大大小为4k)。

Of course, such server global dict should be carefully guarded for growing too large, being in-memory. Some sort of expiring the 'old' key/value pairs can be coded during request processing.

当然,这样的服务器全局 dict 应该小心保护,以免变得太大,在内存中。可以在请求处理期间对某种过期的“旧”键/值对进行编码。

Again, not recommended for production or scalable deployments, but possibly ok for local task oriented servers where separate db is too much for the given task

同样,不建议用于生产或可扩展部署,但可能适用于本地面向任务的服务器,其中单独的 db 对于给定任务来说太多了

...

...