简体   繁体   中英

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.

threading.Lock seems to be an implementation of _dummy_thread.LockType linked above.

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?

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

It does not have much though, as the actual implementation is done in C, in the _thread module. It is selected at python interpreter's compile time. You can find here the implementation for pthread and for windows .

Namely, the Windows implementation uses WaitForSingleObjectEx and pthread implementation uses sem_timedwait under the hood.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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