In a multithreaded environment, each thread has its own data. It is better for a thread to use its own local variables than to use global variables, because local variables are visible only to the thread itself and do not affect other threads, and changes to global variables must be locked.
The problem with local variables, however, is that they are cumbersome to pass when a function is called:
def process_student(name) :
std = Student(name)
# STD is a local variable, but every function uses it, so it must be passed:
do_task_1(std)
do_task_2(std)
def do_task_1(std) :
do_subtask_1(std)
do_subtask_2(std)
def do_task_2(std) :
do_subtask_2(std)
do_subtask_2(std)
Copy the code
Is it okay if every function is called layer by layer? Using global variables? Neither, because each thread handles a different Student object and cannot be shared. Need Python learning materials can 76 24 months 59 test 510 points remove the text plus my micro, information free to everyone! (There are too many information books, SO I just send some randomly!)
What if a global dict holds all Student objects, and the thread itself is the key to get the corresponding Student object?
global_dict = {}
def std_thread(name) :
std = Student(name)
Add STD to global_dict:
global_dict[threading.current_thread()] = std
do_task_1()
do_task_2()
def do_task_1() :
Instead of passing in STD, look for it based on the current thread:
std = global_dict[threading.current_thread()]
...
def do_task_2() :
Any function can find the STD variable of the current thread:
std = global_dict[threading.current_thread()]
...
Copy the code
This approach is theoretically possible, and its biggest advantage is that it eliminates the problem of passing STD objects through functions at each level, but the code for each function to fetch STD is a bit ugly.
Is there an easier way?
Instead of looking up a dict, ThreadLocal does this for you automatically:
import threading
Create a global ThreadLocal object:
local_school = threading.local()
def process_student() :
print 'Hello, %s (in %s)' % (local_school.student, threading.current_thread().name)
def process_thread(name) :
# bind student to ThreadLocal:
local_school.student = name
process_student()
t1 = threading.Thread(target= process_thread, args=('Alice',), name='Thread-A')
t2 = threading.Thread(target= process_thread, args=('Bob',), name='Thread-B')
t1.start()
t2.start()
t1.join()
t2.join()
Copy the code
Execution Result:
Hello, Alice (in Thread-A)
Hello, Bob (in Thread-B)
Copy the code
The local_school global variable is a ThreadLocal object that can read and write student attributes from each Thread, but does not affect each other. You can think of local_school as a global variable, but each property, such as local_school.student, is local to the thread and can be read or written to without interfering with each other. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can price @762459510, that is really very good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~
The global variable local_school is a dict. You can use local_school.student and bind other variables, such as local_school.teacher.
The most common use of ThreadLocal is to bind each thread to a database connection, HTTP request, user identity, etc., so that all handlers called by a thread can easily access these resources.