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