The threadlocal in Python

Source: Internet
Author: User

In multi-threading, locking is required for operations that share data in common.

However, for local variables , each thread is independent of each other.

If the thread t want to F1 the function in the local variable V1 to the function F2, F2 again want to put this variable to F3, a layer of a layer of the more cumbersome to pass.

Name the object returned by Python's threading.local () tl, place the TL at the Global location, and bind the properties to the TL in each function.

TL can be regarded as a dictionary, the key value is the thread ID, value is a dictionary , value represents the dictionary contains our bound properties.

The question here is, if the thread T1 knows the ID of the thread T2, wouldn't it be possible to modify the thread T2 data in threads T1? This involves a thread-isolation problem.

The actual threading.local () dealt with the situation, and the principle was later studied.

This way, when multithreaded execution, by using threading.local (), for each thread, you can easily access the data of their own thread anywhere ,

Each thread is isolated from each other and does not interfere with each other.

The threadlocal in Python

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.