Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

More Locks

Give Feedback

In this lesson, you’ll continue exploring locks and learn about deadlock and how to avoid it. A lock is also called a mutex in other languages. If you download the sample code, you can get your own copy of 11-more_locks.py:

Download

Sample Code (.zip)

12.9 KB

To learn more, you can also check out the documentation on threading.RLock and threading.current_thread as well as the Wikipedia page for deadlock.

Anonymous on Nov. 21, 2019

Thank you for this very useful course. You explained the reentrant lock syntax but you didn’t explain where it is useful, what situation would requires a reentrant lock?

Thanks

Lee RP Team on Nov. 21, 2019

I’m glad you found this course useful. Good question about the use case of reentrant locks. RLocks are used in the same way as regular Locks, except that a thread can acquire more than one lock if it’s an RLock. When you use RLocks you are less likely to run into deadlock situations where a thread tries to acquire the lock more than once. A user named Erik on StackOverflow also gave another use case:

suppose you acquire a granular lock (ie: on a file) and do something fairly expensive (ie: decrypting it). an rlock prevents deadlocks from forming in a complex series of dependencies that, also, may try to acquire the same lock.

Hope this helps, Lee

Become a Member to join the conversation.