Analysis of variable problems in multiple threads of Python

Source: Internet
Author: User
This article mainly introduces the variable problem in Python multithreading. due to the existence of GIL, the multi-thread programming problem in Python has always been a hot topic among developers. if you need it, you can refer to it in the multi-thread 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 can only be seen by the thread itself and will not affect other threads, and modifications to global variables must be locked.

However, there is also a problem with local variables, that is, when the function is called, it is very troublesome to pass:

Def process_student (name): std = Student (name) # std is a local variable, but every function must use it. Therefore, it must be passed in: 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)

What if the parameter is passed through one layer of each function? Using global variables? No, because each thread processes different Student objects and cannot share them.

What if we use a global dict to store all the Student objects and use the thread itself as the key to get the Student object corresponding to the thread?

Global_dict = {} def std_thread (name): std = Student (name) # place std in the global variable global_dict: global_dict [threading. current_thread ()] = std do_task_1 () do_task_2 () def do_task_1 (): # search by the current thread instead of passing in std: 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 ()]...

This method is theoretically feasible. its biggest advantage is that it eliminates the issue of passing std objects in each layer of functions. However, the code for getting std from each function is a bit ugly.

Is there a simpler way?

ThreadLocal came into being, so you don't need to find dict. ThreadLocal will help you do this 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): # student bound 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 ()

Execution result:

Hello, Alice (in Thread-A)Hello, Bob (in Thread-B)

The global variable local_school is a ThreadLocal object. each Thread can read and write the student attribute, but it does not affect each other. You can regard local_school as a global variable, but each attribute, such as local_school.student, is a local variable of the thread. it can be read and written without interfering with each other and does not need to manage the lock. ThreadLocal will handle it internally.

It can be understood that the global variable local_school is a dict. it can not only use local_school.student, but also bind other variables, such as local_school.teacher.

ThreadLocal is most commonly used to bind a database connection, HTTP request, and user identity information to each thread, in this way, all the called processing functions of a thread can easily access these resources.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.