threading.Lock.acquire() 在此实现中如何阻塞?

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

在 Python 3.4.3 上,我无法理解 threading.Lock.acquire() 是如何阻塞的,直到锁定状态设置为解锁。

threading.Lock 似乎是上面链接的 _dummy_thread.LockType 的实现。

这段代码似乎除了阻塞之外什么都没做。如果我是正确的,threading.Lock?

的阻塞行为的实现在哪里

您链接的函数是接口的 dummy 实现。这意味着它什么都不做。没有阻塞。当您实际上不使用线程时使用它,以简化可以在线程和非线程环境中工作的代码的编写。

你想看的函数在threading.py

它并没有太多内容,因为实际的实现是在 _thread 模块中用 C 语言完成的。它是在 python 解释器的编译时选择的。您可以在此处找到 pthread and for windows.

的实现

也就是说,Windows 实现在幕后使用 WaitForSingleObjectEx and pthread implementation uses sem_timedwait