[Python] threading local thread small variable test

Source: Internet
Author: User

[Python] threading local thread small variable test
Concept

There is a concept called a local variable in a thread. Generally, we lock global variables in multiple threads. Such variables are shared variables, and each thread can read and write variables, in order to keep the synchronization, we will handle the shackles. However, after some variables are initialized, we only want to keep them in every thread, which is equivalent to a shared variable in a thread and is isolated between threads. The python threading module provides such a class called local.

 

The differences between shared variables and local variables in multiple threads I will draw two small pictures to briefly describe them (the drawing capability is average. Please forgive me. There are many good articles on conceptual things)

Global Variables

 

Thread Local variable

 

 

Comparison:

The following are three small examples: use local variables, global variables, and local in gevent to check whether the thread is isolated from variable operations.

 

#! /Usr/bin/python #-*-coding: UTF-8-*-# python2.7x # author: orangelliu # date: ''' after the initial value of the local variable of the thread is defined, in the Thread, isolation can be maintained. For comparison, gevent threads compare the local variables '''from time import sleepfrom random import randomfrom threading import Thread, localdata = local () with global variables respectively () 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.6 s]

We can see that each v only operates in its own thread.

 

 

#! /Usr/bin/python #-*-coding: UTF-8-*-# python2.7x # author: orangelliu # date: ''' after the initial value of the local variable of the thread is defined, in the Thread, isolation can be maintained. For comparison, gevent threads compare the global variable ''' from time import sleepfrom random import randomfrom threading import Thread with global variables respectively, local # If global variables are used, def bar1 (): global v print 'calledddddd from % s' % vdef foo1 (): global 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 ..........
<G id = "1"> Finished in 1.8 s </G>

We can see that the value of v has been operated by two threads. The final thread 2 is just a process of re-spelling strings, because thread 1 has created the global variable v

 

 

#! /Usr/bin/python #-*-coding: UTF-8-*-# python2.7x # author: orangelliu # date: ''' after the initial value of the local variable of the thread is defined, in the thread, isolation can be maintained. For comparison, gevent threads compare gevent coroutine ''' import geventfrom gevent with global variables respectively. 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. spawn (foo, '1') g2 = gevent. spawn (foo, '2') gevent. joinall ([g1, g2])

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

 

 

Similar to the thread result.

 

 

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.