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

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

问题描述

我正在阅读Flask文档,并且阅读了此内容-

I am reading the Flask documentation 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 .您可以通过在Python交互式解释器中输入import _threading_local; help(_threading_local)来获得更多细节.

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时,您都可以在线程(或进程或事件let)中安全使用数据结构,而不必担心锁定和其他问题.并发问题.

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天全站免登陆