TLS and Python thread local

Source: Internet
Author: User

Tls

First say TLS (Thread Local Storage), which is explained on the wiki:

thread-local Storage (TLS) is a computer programming method, uses static or global memory local to a thread.

Thread Local Storage (TLS) is a computer programming technique that uses static or global memory to store thread-local variables (transliteration).

The goal is to implement variable isolation, the "Same" global variable, which can have different values for different threads (similar to the concept of a copy). Refer to the following Python implementations and descriptions.

Python thread Local

Import threadingglobal_storage=threading.local ()

 

Copy action

#-*-coding:utf-8-*-import threadingglobal_storage = threading.local () import Timedef test_local (): Time.sleep (1) if Hasattr (Global_storage, ' x '): Global_storage.x+=1else:global_storage.x=0print global_storage.xif __name__== ' __main __ ': For I in range (3): Threading. Thread (target=test_local). Start ()

The above code will output 3 0 (X has three copies)

Instead of 0,1,2 (x is not the only single case)

Of course, for a single thread, thread local is weakened for a global pool of variables .

Reprint please specify this article from: http://www.cnblogs.com/Tommy-Yu/p/5459291.html, thank you!

TLS and Python thread local

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.