Skip to main content

A wonderful life without deadlocks

Project description

logo

Downloads Downloads codecov Lines of code Hits-of-Code Test-Package Python versions PyPI version Checked with mypy Ruff

Deadlocks are the most terrible enemies of all programmers who make multithreaded programs. If you are a one of them - this library is going to help you out.

How can I use it?

Get the locklib from the pip:

$ pip install locklib

And use a lock from this library as a usual Lock from the standard library:

from threading import Thread
from locklib import SmartLock


lock = SmartLock()
counter = 0

def function():
  global counter

  for _ in range(1000):
      with lock:
          counter += 1

thread_1 = Thread(target=function)
thread_2 = Thread(target=function)
thread_1.start()
thread_2.start()

assert counter == 2000

In this case the lock helps us not to get a race condition, as the standard Lock does. But! Let's trigger a deadlock and look what happens:

from threading import Thread
from locklib import SmartLock


lock_1 = SmartLock()
lock_2 = SmartLock()

def function_1():
  while True:
    with lock_1:
      with lock_2:
        pass

def function_2():
  while True:
    with lock_2:
      with lock_1:
        pass

thread_1 = Thread(target=function_1)
thread_2 = Thread(target=function_2)
thread_1.start()
thread_2.start()

And... We have an exception like this:

...
locklib.errors.DeadLockError: A cycle between 1970256th and 1970257th threads has been detected.

Deadlocks are impossible for this lock!

If you want to catch the exception, import this from the locklib too:

from locklib import DeadLockError

How does it work?

Deadlock detection based on Wait-for Graph.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

locklib-0.0.12.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

locklib-0.0.12-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page