简体   繁体   English

这个实现中的threading.Lock.acquire()如何阻塞?

[英]How is threading.Lock.acquire() blocking in this implementation?

On Python 3.4.3, I'm failing to understand how the threading.Lock.acquire() blocks until the lock status is set to unlocked. 在Python 3.4.3上,我无法理解threading.Lock.acquire()在阻塞状态设置为解锁之前如何阻塞。

threading.Lock seems to be an implementation of _dummy_thread.LockType linked above. threading.Lock似乎是上面链接的_dummy_thread.LockType的实现。

This code seems to be doing anything but blocking. 这段代码似乎除了阻塞之外什么都没做。 If I am correct, where is the implementation of the blocking behavior of a threading.Lock? 如果我是正确的,则threading.Lock的阻塞行为的实现在哪里?

The function you linked is a dummy implementation of the interface. 您链接的功能是该接口的虚拟实现。 That means it does nothing. 这意味着它什么也不做。 No blocking. 无阻塞。 It is used when you do not actually use threads, to ease the writing of code that can work both in threaded and non-threaded environments. 当您实际上不使用线程时,可以使用它来简化可在线程和非线程环境中工作的代码的编写。

The function you want to look at is in threading.py 您要查看的功能在threading.py中

It does not have much though, as the actual implementation is done in C, in the _thread module. 但是,由于实际的实现是在C中的_thread模块中完成的,因此它没有多少。 It is selected at python interpreter's compile time. 它是在python解释器的编译时选择的。 You can find here the implementation for pthread and for windows . 您可以在此处找到pthreadWindows的实现

Namely, the Windows implementation uses WaitForSingleObjectEx and pthread implementation uses sem_timedwait under the hood. 即,Windows实现使用WaitForSingleObjectEx,而pthread实现使用sem_timedwait

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM