Skip to main content

A wonderful life without deadlocks

Project description

locklib

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.4.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

locklib-0.0.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file locklib-0.0.4.tar.gz.

File metadata

  • Download URL: locklib-0.0.4.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.2

File hashes

Hashes for locklib-0.0.4.tar.gz
Algorithm Hash digest
SHA256 772eb43f5aac1e412cfc60bd1b969067b803a880d4d5e948d8c1e8bc66e22800
MD5 8418f1ab83fbfe6512bfba25ac59ee31
BLAKE2b-256 1e52565f7fee9272ea2b2b59298f4a612cb9684561bf1ed439e82d6b96a7a303

See more details on using hashes here.

File details

Details for the file locklib-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: locklib-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.2

File hashes

Hashes for locklib-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ae19bfa9a178ef874bb06f45bbed56d6745a27fbc590b894235551f230271c59
MD5 b0a53d46f23faf4e214b210e9911904d
BLAKE2b-256 9c891e719e2e3ed1a3d9ec6bbab2cd4fa4a38ed5341fcc4b244e9e9b1defac5d

See more details on using hashes here.

Supported by

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