Why do we need locks?

When you have more than one thread, then you may need to consider how to avoid conflicts. What we mean by this is that you may have a use case where more than one thread will need to access the same resource at the same time. If you don’t think about these issues and plan accordingly, then you will end up with some issues that always happen at the worst of times and usually in production.

The solution is to use locks. A lock is provided by Python’s threading module and can be held by either a single thread or no thread at all. Should a thread try to acquire a lock on a resource that is already locked? That thread will basically pause until the lock is released.

Get hands-on with 1200+ tech skills courses.