Python Learning note __10.3 ThreadLocal

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

1 , overview

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 can only be seen by the thread themselves and not affect other threads, and changes to global variables must be locked. But a thread's local variable, which is cumbersome when the function is called

1) Way one: one layer at a pass

def process_student (name):

std = Student (name) # std is a local variable, but each function uses it, so 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)

2) mode two: Use a global dict to store all the student objects, and then take the thread itself as the key to get the student object corresponding to threads

Global_dict = {}

def std_thread (name):

std = Student (name)

Global_dict[threading.current_thread ( )] = STD # takes the thread itself as a key and puts the STD into the global variable Global_ In Dict

do_task_1 ()

Do_task_2 ()

Def do_task_1 ():

std = global_dict[threading.current_thread ()] # does not pass in STD, but is found based on the current thread

def do_task_2 ()

std = global_dict[threading.current_thread ()] # Any function can find out the STD variable of the current thread

3) Way three:ThreadLocal

Import threading

Local_school = threading.local () # Create a global threadlocal object

Def process_student ():

std = local_school.student # Gets the current thread-bound variable in s-tudent

print (' Hello,%s (in%s) '% (Std, Threading.current_thread (). Name))

def process_thread (name):

local_school.student = name # for Threadlocal plus s Tudent property, binding thread variable

process_student () # Call function

T1 = Threading. Thread (target= process_thread, args= (' Alice ',), name= ' thread-a ') # Create threads, thread 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. You can think of it as a global variable. Any thread-bound variable is on its different properties. To achieve the effect of non-interference.

2 , Summary

The most common place for ThreadLocal is to bind a database connection, HTTP request, user identity information, and so on for each thread, so that all the calls to the handler for a thread can be accessed easily.

Although a threadlocal variable is a global variable, each thread can only read and write independent copies of its own thread, without interfering with each other. Threadlocal solves the problem of passing arguments between functions in a thread.


Python Learning note __10.3 ThreadLocal

Related Article

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.