python中rlock
If you try to run the code provided below, the lock object will acquire the lock the first time acquire()
method is called but not the second time.
如果尝试运行下面提供的代码,则锁对象将在第一次调用acquire()
方法时acquire()
该锁,但在第二次调用时不会。
Why is that? Well because the normal lock object once acquired cannot be re-acquired even if the same threads attempts to do so.
这是为什么? 好吧,因为即使相同的线程尝试重新获取普通的锁定对象,也无法重新获取它。
But why would someone try to call the acquire()
method twice? Let's take a simple example to demonstrate this simple locking issue:
但是为什么有人会尝试两次调用acquire()
方法呢? 让我们以一个简单的例子来演示这个简单的锁定问题:
lock = threading.Lock()
def get_first_line():
lock.acquire()
try:
# read some file and get the first line
finally:
lock.release()
return data
def get_second_line():
lock.acquire()
try:
# read the same file and get the second line
finally:
lock.release()
return data
In the code above we have two different functions reading different parts of data from a shared resource. We have used locking mechanism to prevent any other thread from modifying the data of the file while our thread is reading it.
在上面的代码中,我们有两个不同的功能,它们从共享资源中读取数据的不同部分。 我们使用了锁定机制来防止任何其他线程在我们的线程读取文件时修改文件的数据。
Now consider that you want to call both the functions one by one, you will do this:
现在考虑要一一调用这两个函数,将执行以下操作:
first = get_first_line()
second = get_second_line()
return first, second
But this call is still not thread safe because while you are reading the data from the shared resource, there can be any other thread which can modify the content of the shared resource in between the two function calls.
但是此调用仍然不是线程安全的,因为当您从共享资源中读取数据时,在两个函数调用之间可以有任何其他线程可以修改共享资源的内容。
To avoid that, we can get a lock and then call these two functions:
为了避免这种情况,我们可以获取一个锁,然后调用以下两个函数:
lock.acquire()
try:
first = get_first_line()
second = get_second_line()
finally:
lock.release()
return first, second
But, this code will not work, because, we will be calling acquire()
on the lock object inside the same thread trying to acquire the lock again inside the functions which we have already acquired before calling the functions.
但是,此代码将不起作用,因为,我们将在同一线程内的锁对象上调用acquire()
,以尝试在调用函数之前已获取的函数内再次获取锁。
Hence in such a situation, the primitive Lock object cannot be used. For such a case we have the RLock
class.
因此,在这种情况下,无法使用原始Lock对象。 对于这种情况,我们有RLock
类。
RLock对象:Python多线程 (RLock Object: Python Multithreading)
An RLock stands for a re-entrant lock. A re-entrant lock can be acquired multiple times by the same thread.
RLock代表可重入锁。 同一线程可以多次获得重入锁。
RLock object also have two methods which they can call, they are:
RLock对象也有两个可以调用的方法,它们是:
the
acquire()
methodacquire()
方法the
release()
methodrelease()
方法
Here is a simple example to demonstrate the working of RLock
object:
这是一个简单的示例来演示RLock
对象的工作方式:
And the code in the simple locking issue example, will also work without any issue if we use the RLock
object:
如果我们使用RLock
对象,那么简单锁定问题示例中的代码也可以正常工作:
lock = threading.RLock()
lock.acquire()
try:
first = get_first_line()
second = get_second_line()
finally:
lock.release()
return first, second
The above code will work fine.
上面的代码可以正常工作。
翻译自: https://www.studytonight.com/python/python-threading-rlock-object
python中rlock