“This is my fourth day of participating in the First Challenge 2022. For more details: First Challenge 2022.”
No. 52 of the official Python column, stop! Don’t miss this zero-based article!
The threading.Lock class was mentioned earlier when the committee introduced thread safety.
Therefore, after acquire, we cannot continue acquire. We must execute a release before other threads can continue acquire.
There is only one thread doing things at a time, this time we look at RLock (ReentrantLock).
What is RLock?
Locks are used to coordinate access to restricted resources, and locks are added to protect access to restricted resources.
But there are clear differences. The first is that RLock can be acquired more than once.
Let’s look at the following code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/24 12:02 am
# @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 negative is the amount transferred out
def transfer(money) :
transfer_only(money)
lock.release()
print("release")
# amount negative is the amount transferred out
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("Student Committee Account Balance:", xuewei_account)
Copy the code
The running effect is as follows:
The transfer_only function has been called twice, but we haven’t released it yet.
There is no blockage.
Let’s show the code that uses Lock:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/24 12:02 am
# @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 negative is the amount transferred out
def transfer(money) :
transfer_only(money)
lock.release()
print("release")
# amount negative is the amount transferred out
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("Student Committee Account Balance:", xuewei_account)
Copy the code
The running effect is as follows:
The only difference is that when creating a lock object, the former is based on the RLock type and the latter is based on the lock (a normal lock).
However, we see that Lock does not allow the thread holding the Lock (the same or another thread) to execute acqure again, and transfer_only executes only once.
Can multiple threads acquire RLock
Try multithread acquire with the following code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/24 12:02 am
# @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 negative is the amount transferred out
def transfer(money) :
transfer_only(money)
lock.release()
print("release")
# amount negative is the amount transferred out
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="Little white thread", target=lambda: transfer_only(100)).start()
threading.Thread(name="Thread flower", target=lambda: transfer_only(-100)).start()
print("-" * 16)
print("Student Committee Account Balance:", xuewei_account)
Copy the code
The running effect is as follows:
Obviously, the second thread is locked.
This conforms to RLock, running the thread that acquired the lock multiple times acqure, but must be released before it can be reassigned to another thread.
The only difference between the two codes is:
In the first section, we acquire RLock objects on the main thread.
transfer_only(100)
transfer_only(-100)
Copy the code
In the second paragraph, we acquire RLock objects multiple times between two different threads. (Although it is not printed out, the thread name is specially set to be different, readers can modify the print check)
Threading. Thread (name = "Thread small white," target = lambda: Transfer_only (100).start() threading.Thread(name=" Thread ", target=lambda: transfer_only(-100)).start()Copy the code
conclusion
In today’s article, we briefly looked at RLock, which is designed to allow us to use locks multiple times within a thread.
It prioritizes locks to the thread holding the lock, reducing thread switching costs, and more importantly, reentrant locks are better at preventing deadlocks. If a thread uses a normal Lock, it will acquire the same Lock. If a thread uses a normal Lock, it will acquire the same Lock.
However, release must be done by the thread that acquired the lock, and acquire must release as many times as possible, which will be shown in the next article.
For those who like Python, please check out the Python Basics section or the Python Getting Started to Master Section
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!