程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python thread 15 reentrant lock RLOCK

編輯:Python

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 ).

RLock What is it? ?

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 .

Can multiple threads all come acquire RLock

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()

summary

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 !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved