Lock and RLock difference python threading module

First, understand what both yes.

The following description with reference to the official website from python

Lock: Lock is called original lock , the lock is a primitive does not belong to a particular thread when locked synchronization primitives component , which is the lowest level of synchronization primitives usable components. Original lock is "locked" or "unlocked" one of two states. It is a non-locked state when it is created. It has two basic methods,  acquire() and  release() . When the non-locked state,  acquire() the locked state and to return immediately. When the state is locked,  acquire() the thread is blocked to other calls  release() to change it to a non-locked state, then  acquire() call the reset its locked state and return. release() Only call in the locked state; it will change the status of a non-locking and returns immediately. If you attempt to release a non-locking lock, thrown  RuntimeError  exception. Lock supports  the context management protocol that is supported with the statement, will be used in the following examples.

RLock: RLock is called reentrant lock to lock the lock, thread calls its  acquire() method; once the thread owns the lock, the method will return. To unlock, thread calls  release() the method. acquire()/ release() to be nested, the lock must be released by reentrant obtain its threads. Once a thread acquires a reentrant lock, it will not get blocked again the same thread. Only the final  release() (outermost pair  release() ) will unlock the lock in order to allow other threads continue to process  acquire() blocked. ; Thread must be released once each time you get it.

Most of the methods used or both the same, the following describe the difference between the two portions of the above highlighted in red

① is the difference between name, called the original lock, called a re-entry lock, this nothing to say

②Lock do not belong to a particular thread when locked, that is to say, Lock can be locked in a thread, unlocked in another thread. For RLock, only the current thread to release a lock on this thread that started the trouble should end it:

 

import threading
import time

lock = threading.Lock()
lock.acquire()

def func():
    lock.release()
    print("lock is released")

t = threading.Thread(target=func)
t.start()

输出结果为:lock is released

 

In the above code, the lock is created in the main thread, and locked, but the lock is released at t thread, the result of normal output, the lock on a thread, can be unlocked by another thread. If the above error lock is changed RLock

③ also the biggest difference between the two, RLock allowed in the same thread was repeatedly acquire in. The Lock does not allow this. In other words, the following scenario for RLock is allowed:

Import Threading 

RLOCK = threading.RLock () 

DEF FUNC ():
     IF # first lock: rlock.acquire ()
         Print ( " First Lock " )
         IF () rlock.acquire: # where the first lock never unlock the second lock is then
             Print ( " sECOND lock " ) 
            rlock.release () # unlock the second lock 
        rlock.release () # unlock the first lock 


T = of the threading.Thread (target = FUNC) 
T. start () 

output:

   first lock
   second lock

Note that the above emphasized the same thread, because only the current thread to release a lock on this thread, and can not have been performed in t1 thread for RLock is the case rlock.acquire, and did not release the lock, another thread t2 also performs rlock.acquire (this condition causes blockage t2)

Well, since a thread can be obtained through a lock Lock, why you should use RLock lock to lock? Consider this situation:

import threading
import time

lock1 = threading.RLock()

def inner():
    with lock1:
        print("inner1 function:%s" % threading.current_thread())

def outer():
    print("outer function:%s" % threading.current_thread())
    with lock1:
        inner()

if __name__ == "__main__":
    t1 = threading.Thread(target=outer)
    t2 = threading.Thread(target=outer)
    t1.start()
    t2.start()
结果:

  outer function:<Thread(Thread-1, started 12892)>
  inner1 function:<Thread(Thread-1, started 12892)>
  outer function:<Thread(Thread-2, started 7456)>
  inner1 function:<Thread(Thread-2, started 7456)>

Look first thread t1, when performing the function of outer, printed first outer function: <Thread (Thread-1, started 12892)>, then the current thread lock1 to a lock, and then performing inner functions, in which inner We need to use lock1 on a lock, if this time with lock it, because they have a lock on, because the program will not get a second lock t1 caused by obstruction, obstruction and after the program can not release the lock, it will lead to deadlock. In this case, RLock comes in handy. t1 and t2 thread execution, as here, just a few more months thread looks cool

 

Guess you like

Origin www.cnblogs.com/olivertian/p/11295031.html