[Python]threading Local Thread variables Quiz

Source: Internet
Author: User

Concept

There is a concept called thread-local variables, generally we are in multi-threaded global variables will be locked processing, this variable is a shared variable , each thread can read and write variables, in order to maintain synchronization we will do the yoke processing. But after some variables are initialized, we just want them to exist in each thread, equivalent to shared variables within a thread, and the threads are quarantined . A class called local is provided in the Python threading module.


The difference between shared variables and local variables in multi-threading I draw two small pictures, simple description under (Drawing Ability General, please forgive me, the concept of things we can Google, a lot of good articles)


Global variables



Thread Local Variables



Contrast:

Here are 3 small examples that use local local variables, global variables, and local gevent to see if the operation of a thread is isolated from a variable.

#!/usr/bin/python#-*-coding:utf-8-*-# python2.7x# author:orangelliu# date:2014-08-20 "Thread local variable  initial value is defined, You can maintain isolation in a thread in order to make comparisons, respectively, and global variables, gevent threads vs. thread Local variables ' from time import sleepfrom random import randomfrom threading Import Thread , Localdata = local () def Bar ():    print ' Called from%s '%data.vdef foo ():    data.v = str (DATA.V) + ' ... '    bar () Class T (Thread):    def run (self):        sleep (Random ())        data.v = Self.getname ()        sleep (1)        foo () T (). Start () T (). Start ()

Execution Result:

Called from Thread-1 ....
Called from Thread-2 ....
[Finished in 1.6s]

You can see that each v operates only on its own thread.


#!/usr/bin/python#-*-coding:utf-8-*-# python2.7x# author:orangelliu# date:2014-08-20 "Thread local variable  initial value is defined, Isolation can be maintained in the thread in order to make comparisons, respectively, and global variables, gevent threads compare global variables ' from time import sleepfrom random import randomfrom threading Import Thread, local# If you use global Variables Def bar1 (): Global    v    print ' calledddddd from%s '%vdef foo1 (): Global    v    v = v + ' ... ' 
   
    BAR1 () class T1 (Thread):    def run (self):        global v        Sleep (random ())        v =self.getname ()        sleep (1)        foo1 () T1 (). Start () T1 (). Start ()
   

Execution Result:

CALLEDDDDDD from Thread-1 .....
CALLEDDDDDD from Thread-1 .....
[Finished in 1.8s

You can see that the V value is being manipulated by 2 threads, and the last thread 2 is just a process of re-spelling the string because thread 1 has created a global variable V


#!/usr/bin/python#-*-coding:utf-8-*-# python2.7x# author:orangelliu# date:2014-08-20 "Thread local variable  initial value is defined, Isolation can be maintained in the thread in order to make comparisons, respectively, and global variables, gevent threads contrast gevent "Import geventfrom gevent.local Import localdata = local () def Bar ():    print ' Called from%s '%data.vdef foo (v):    data.v = v    data.v = str (DATA.V) + ' ... '    bar () G1 = Gevent.spa WN (foo, ' 1 ') g2 = Gevent.spawn (foo, ' 2 ') Gevent.joinall ([G1, G2])

Called from 1 .....
Called from 2 .....
[Finished in 0.1s]


The result of a similar thread.


This article is derived from"Orangleliu Notebook" Blog, be sure to keep this source http://blog.csdn.net/orangleliu/article/details/38741275
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.