Python Advanced (3) _ Process and thread lock (mutex, recursive lock, Semaphore)

Source: Internet
Author: User
Tags mutex semaphore

1. Sync Lock (Lock)

Data disturbances occur when individual threads need access to a common resource

For example:

1 Import Threading,time2 def Sub ():3     GlobalNum #对全局变量进行操作4 5temp=Num6Time.sleep (0.001) #模拟线程执行中出现I/o delay, etc.7num=temp-1#所有线程对全局变量进行减一8 9Time.sleep (1)Ten  Onenum= - AL=[] -  -  forIinchRange -): theT=threading. Thread (target=sub,args=()) - T.start () - l.append (t) -  +  forObjinchL: - Obj.join () +  A print (num) at  - #执行结果不可预期: ->>: - ->>: the ->>: the - 

  When global resources (counter) are preempted, the problem arises because there is no control over the access of multiple threads to the same resource, causing damage to the data and making the results of the thread run unpredictable. This behavior is called "Thread insecurity." In the development process we have to avoid this situation, how to avoid it? This is the use of mutual exclusion lock.

The concept of mutex lock

In Python programming, the concept of object mutexes is introduced to ensure the integrity of shared data operations. Each object corresponds to a tag that can be called a "mutex", which is used to guarantee that only one thread can access the object at any one time. In Python we use the lock class provided by the threading module.

We have to rectify the above program, for which we need to add a mutex lock variable lock = threading. Lock (), and then we will preempt this lock lock.acquire () before we scramble for resources, and we release this lock Mutex.release () after the use of the resource is complete.

The code is as follows:

Import Threading,timedef Sub ():    Global num    lock.acquire ()    temp=num    time.sleep (0.01)    num= Temp-1    lock.release ()    time.sleep (1) num=100l=[]lock=threading. Lock () for I in range:    t=threading. Thread (target=sub,args= ())    T.start ()    l.append (t) for obj in L:    obj.join () print (num)
2. Deadlock and Recursive lock

The so-called deadlock: refers to two or two or more processes or threads in the execution process, because of the contention for resources caused by a mutual waiting phenomenon, if there is no external force, they will not be able to proceed. At this point the system is in a deadlock state or the system generates a deadlock, and these processes, which are always waiting on each other, are called deadlock processes.

Examples of deadlocks are generated:

Class MyThread (threading. Thread):    def __init__ (self):        threading. Thread.__init__ (self)    def run (self):        Self.foo ()    def foo (self):        locka.acquire ()        print (' I am%s Get locka---------%s '% (Self.name,time.ctime ()))        Lockb.acquire ()        print (' I am%s GET lockb---------%s '% ( Self.name, Time.ctime ()))        lockb.release ()        locka.release () locka=threading. Lock () lockb=threading. Lock () for I in range:    t=mythread ()    T.start ()
use recursive locks to resolve:

In Python, in order to support multiple requests for the same resource in the same thread, Python provides a reentrant lock rlock. The Rlock internally maintains a lock and a counter variable, counter records the number of acquire, so that resources can be require multiple times. Until all the acquire of a thread are release, the other threads can get the resources. In the example above, if you use Rlock instead of lock, a deadlock will not occur:

Class MyThread (threading. Thread):    def __init__ (self):        threading. Thread.__init__ (self)    def run (self):        Self.foo ()        Self.bar ()    def foo (self):        Rlock.acquire ()        print (' I am%s GET locka---------%s '% (Self.name,time.ctime ()))        Rlock.acquire ()        print (' I am%s GET lockb---------%s '% (Self.name, Time.ctime ()))        rlock.release ()        Rlock.release ()    def Bar (self):        rlock.acquire ()        print (' I am%s GET lockb---------%s '% (Self.name, Time.ctime ()))        Time.sleep (1)        rlock.acquire ()        print (' I am%s GET locka---------%s '% (Self.name, Time.ctime ()))        rlock.release ()        rlock.release () rlock=threading. Rlock () for I in range:    t=mythread ()    T.start ()

  

3, Semaphore (signal volume)

Semaphore manages a built-in counter,
Built-in counter whenever acquire () is called-1;
Built-in counter +1 when call Release ();
The counter cannot be less than 0, and when the counter is 0 o'clock, acquire () blocks the thread until another thread calls release ().

Example: (at the same time only 5 threads can get semaphore, that is, you can limit the maximum number of connections to 5):

1 Import Threading2 Import Time3 4Semaphore = Threading. Semaphore (5)5 6 def func ():7     ifSemaphore.acquire ():8Print (Threading.currentthread (). GetName () +'Get Semaphore')9Time.sleep (2)Ten semaphore.release () One  A  forIinchRange -): -T1 = Threading. Thread (target=func) -T1.start ()

Python Advanced (3) _ Process and thread lock (mutex, recursive lock, Semaphore)

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.