ceremonial Python Column No 52 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !
The previous school committee introduced Thread safe time , Mentioned threading.Lock This class .
This is why we acquire after , Can't continue acquire 了 , It has to be done once release, Other threads can continue acquire.
There is only one thread doing things at a time , This time we'll see RLock(ReentrantLock, Reentrant lock ).
Simple understanding , It goes with Lock similar , Are used to coordinate access to restricted resources , Lock to protect access to restricted resources .
however , There are still obvious differences between them . The first is ,RLock Can be acquire many times .
Let's look at the code below :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/24 12:02 In the morning
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello
import random
import threading
import datetime
import time
xuewei_account = 100
lock = threading.RLock()
# amount A negative number is the transfer out amount
def transfer(money):
transfer_only(money)
lock.release()
print("release")
# amount A negative number is the transfer out amount
def transfer_only(money):
lock.acquire()
print("transfer now")
global xuewei_account
for x in range(100000):
xuewei_account += money
print("transfer done")
transfer_only(100)
transfer_only(-100)
print("-" * 16)
print(" The balance of the school committee's account :", xuewei_account)
The operation effect is as follows :
transfer_only The function was called twice , But we haven't release too .
The whole process is not blocked .
We use Lock Show me the code of :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/24 12:02 In the morning
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello
import random
import threading
import datetime
import time
xuewei_account = 100
lock = threading.Lock()
# amount A negative number is the transfer out amount
def transfer(money):
transfer_only(money)
lock.release()
print("release")
# amount A negative number is the transfer out amount
def transfer_only(money):
lock.acquire()
print("transfer now")
global xuewei_account
for x in range(100000):
xuewei_account += money
print("transfer done")
transfer_only(100)
transfer_only(-100)
print("-" * 16)
print(" The balance of the school committee's account :", xuewei_account)
The operation effect is as follows :
The difference between two break codes , Just for creation lock When the object , The former is based on RLock type , The latter is based on Lock( Ordinary locks ).
But we see Lock, Threads holding locks are not allowed ( Same or other thread ) Again acqure,transfer_only Only once .
Try multiple threads acquire, The code is as follows :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/24 12:02 In the morning
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello
import random
import threading
import datetime
import time
xuewei_account = 100
lock = threading.RLock()
# amount A negative number is the transfer out amount
def transfer(money):
transfer_only(money)
lock.release()
print("release")
# amount A negative number is the transfer out amount
def transfer_only(money):
lock.acquire()
print("transfer now")
global xuewei_account
for x in range(100000):
xuewei_account += money
print("transfer done")
threading.Thread(name="thread The small white ", target=lambda: transfer_only(100)).start()
threading.Thread(name="thread floret ", target=lambda: transfer_only(-100)).start()
print("-" * 16)
print(" The balance of the school committee's account :", xuewei_account)
The operation effect is as follows :
Obviously , The second thread is locked .
This is in line with RLock Set up , The thread that gets the lock runs multiple times acqure, But must be release Then it can be reassigned to other threads .
The difference between two break codes is only :
The first paragraph , We are in the main thread , That is, the same thread multiple times acquire RLock object .
transfer_only(100)
transfer_only(-100)
The second paragraph , We are between two different threads many times acquire RLock object .( Although it didn't print out , But the thread name here is also deliberately set differently , Readers can modify, print, check )
threading.Thread(name="thread The small white ", target=lambda: transfer_only(100)).start()
threading.Thread(name="thread floret ", target=lambda: transfer_only(-100)).start()
Today we have a brief understanding of RLock, Its design allows us to use locks multiple times in a thread .
It gives priority to the thread that holds the lock , Reduce the consumption of thread switching , what's more , Reentrant lock design is more conducive to preventing deadlock .( Here's a simple example , Imagine , A thread invokes multiple methods in acquire Same lock , At this time, use ordinary Lock Will enter a deadlock state , The school committee will write another article later )
however release The thread that must acquire the lock , and acquire A few times you have to release A few times , This next article will show .
like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column
Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !