Skip to main content

Systemwide pseudo-atomic lock mechanism for Python

Project description

CryoLock

The advisory file lock mechanism for the paranoid; designed to work under restrictive environments.

CryoLock is a library for file-based locking of critical sections. It's primarily developed for applications that make changes to critical files as it ensures that two instances of the same application won't interfere with one another or corrupt said critical files. A CryoLock is essentially Python's threading.Lock and multiprocessing.Lock but for threads and processes that can't interact with each other and therefore can't be provided with a shared lock instance.

  • Completely systemwide. Can be used to enforce mutual exclusion even if the processes trying to acquire the lock are completely independent from one another.
  • Cross-platform. Works in any environment where the required functions are available in the Python standard library (namely os.urandom and os.utime).
  • Logically atomic. There are no race conditions that could allow multiple parallel threads to acquire the same lock; even if the underlying OS doesn't offer any atomic FS operations.
  • Portable. Written in pure Python and without any external dependencies.
  • Anywhere. Supports Python 2.7 as well as Python 3.

Usage

Executing help(__import__('cryolock')) in a Python console should provide some documentation; but here are some templates:

"""Simple example."""

import os
from cryolock import Lock

# Do normal stuff here...

with Lock(os.path.join(os.getcwd(), 'my-simple-lock.cryolock')):
    # Do critical stuff here...

# Continue doing normal stuff here...
"""Example with a `cryolock.Lock` instance being shared between multiple threads."""

import os
from cryolock import Lock as IOLock
from threading import Lock as ThreadLock, Thread

def thread_function(thread_lock, io_lock):
    # Do normal stuff here...

    # An acquired `cryolock.Lock` instance EXCLUSIVELY stops OTHER `cryolock.Lock` instances from accessing a
    # critical region; so utilizing a `threading.Lock` is required if you're gonna be sharing a `cryolock.Lock`
    # instance between multiple threads.
    with thread_lock:
        with io_lock:
            # Do critical stuff here...

    # Continue doing normal stuff here...

tl = ThreadLock()
iol = IOLock(os.path.join(os.getcwd(), 'my-shared-lock.cryolock'))
for _ in range(5):
    Thread(target=thread_function, args=(tl, iol))

Cloning and Contributing

This repository can be cloned and accepts issues and pull requests:

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

cryolock-1.0.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

cryolock-1.0.0-py2.py3-none-any.whl (22.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cryolock-1.0.0.tar.gz.

File metadata

  • Download URL: cryolock-1.0.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for cryolock-1.0.0.tar.gz
Algorithm Hash digest
SHA256 59c1ff9d28ce26a484204f1c0d03c373654599ff07682ccebe91318b1e9035c1
MD5 07877fca5b60059f125598b9126e483d
BLAKE2b-256 32326b7f5c1262c1c77aa9f597d586ee6cb22265e5546f8f517c08f85c1d228a

See more details on using hashes here.

File details

Details for the file cryolock-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cryolock-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for cryolock-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 77cea53c4cdbab097e645cd80691580dd8ebc680b89828ff7091d7beab7d95f4
MD5 276396ebd023cd48da5b6192f4c0e554
BLAKE2b-256 3733e6bd10324b4cf9520ce163bd901b1c0c640ddf7bd9ab25b119dcdec4340f

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