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.
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.

Lock Objects

Give Feedback

In this lesson, you’ll learn about lock objects and how you can use a lock as a context manager to prevent more than one thread from accessing a part of your program at the same time. If you download the sample code, you can get your own copy of 10-lock.py:

Download

Sample Code (.zip)

12.9 KB

To learn more, you can also check out the documentation.

Comments & Discussion

malbert137 on June 1, 2020

Technically, there is no guarantee that the the “withdraw” thread acquires the lock before the “deposit” thread, but this should be very rare in practice. In real life, the ordering of the deposit and withdraw would be determined by the account holders (e.g., perhaps the account is held by two people, and one is making a deposit at one location while the other is trying to make a withdraw at another location), so the code wouldn’t be sequencing those actions. In any case, there is no check for overdraw.

Become a Member to join the conversation.