I am reading the Flask documentation (specifically, the Foreword for Experienced Programmers chapter) and I read this -
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 -
Thread local can be considered as a scope of access like session scope or request scope. In thread local, you can set any object and this object will be local and global to the specific thread which is accessing this object. Java ThreadLocal class provides thread-local variables.
Thread-local data is data whose values are thread specific. To manage thread-local data, just create an instance of local (or a subclass) and store attributes on it: mydata = threading.local() mydata.x = 1. The instance's values will be different for separate threads.
As of Flask 1.0, flask server is multi-threaded by default. Each new request is handled in a new thread. This is a simple Flask application using default settings.
Thread Local Storage (TLS) is the method by which each thread in a given multithreaded process can allocate locations in which to store thread-specific data. Dynamically bound (run-time) thread-specific data is supported by way of the TLS API (TlsAlloc).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With