Flask 中的线程本地对象是什么意思? [英] What does Thread Local Objects mean in Flask?

查看:34
本文介绍了Flask 中的线程本地对象是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Flask 文档(特别是经验丰富的程序员的前言一章) 并且我读了这个 -

I am reading the Flask documentation (specifically, the Foreword for Experienced Programmers chapter) and I read this -

Flask 的设计决策之一是简单的任务应该简单;它们不应占用大量代码,但不应限制您.正因为如此,Flask 的设计选择很少,有些人可能会觉得奇怪或不正统.例如,Flask 在内部使用线程本地对象,因此您不必在请求中从一个函数到另一个函数传递对象以保持线程安全.这种方法很方便,但需要一个有效的请求上下文来进行依赖注入,或者在尝试重用使用与请求挂钩的值的代码时.Flask 项目对线程局部变量很诚实,没有隐藏它们,并在代码和文档中标明使用它们的地方.

One of the design decisions in Flask was that simple tasks should be simple; they should not take a lot of code and yet they should not limit you. Because of that, Flask has few design choices that some people might find surprising or unorthodox. For example, Flask uses thread-local objects internally so that you don’t have to pass objects around from function to function within a request in order to stay threadsafe. This approach is convenient, but requires a valid request context for dependency injection or when attempting to reuse code which uses a value pegged to the request. The Flask project is honest about thread-locals, does not hide them, and calls out in the code and documentation where they are used.

这是什么意思?具体以下问题-

What does this mean? Specifically the following questions -

  • 什么是线程本地对象?它们的使用方式和时间以及它们的用途是什么?
  • 在内部使用线程本地对象如何确保线程安全,以及如何将对象传递给函数导致非线程安全?
  • 在这种情况下,有效请求上下文的含义是什么?

推荐答案

线程本地对象是存储在专用结构中的对象,与当前线程 ID 相关联.如果您向该结构询问对象,它将使用当前线程标识符为您提供当前线程独有的数据.请参阅threading.local.您仍然可以通过输入 import _threading_local; 获得更多详细信息.help(_threading_local) 进入你的 Python 交互式解释器.

A thread-local object is an object that is stored in a dedicated structure, tied to the current thread id. If you ask this structure for the object, it'll use the current thread identifier to give you data unique to the current thread. See threading.local. You can get more detail still by entering import _threading_local; help(_threading_local) into your Python interactive interpreter.

这意味着每当您使用 current_appgrequests 时,您都会获得一个可以在您的线程中安全使用的数据结构(或process 或 eventlet),而无需担心锁定和其他并发问题.

This means that whenever you use current_app, g or requests you get a data structure that is safe to use in your thread (or process, or eventlet), without you having to worry about locking and other concurrency issues.

在正常操作中,Flask 处理传入的 WSGI 请求;对于每个此类请求,都会为您创建一个请求上下文;这由 grequest 对象表示.如果您尝试在没有传入请求的情况下使用您的任何视图(例如,在您的测试中),则 request 对象将不起作用并抱怨没有有效的请求上下文.在这种情况下,Flask 为您提供了按需生成此类上下文的工具.请参阅伪造资源和上下文文档,以及请求上下文一章.

In normal operation, Flask handles incoming WSGI requests; for each such request a request context is created for you; this is represented by the g and request objects. If you are trying to use any of your views without an incoming request (say, in your tests), then the request object will not work and complain that there is no valid request context. Flask provides you with the tools to produce such a context on demand in that case. See the Faking Resources and Context documentation, as well as the Request Context chapter.

这篇关于Flask 中的线程本地对象是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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