Skip to main content

Provide locks with interface similar to those from threading and multiprocessing, which are applicable in other situations.

Project description

locking

build status

  1. Overview
  2. Examples
  3. Installation

Overview

These locks provide a similar interface to those in the threading and multiprocessing modules with a different set of tradeoffs. These locks are useable by multiple different processes when those processes agree on a naming scheme. This can be used in order to allow multiple processes running on the same machine to semi-coordinate or in the case of the redis or dynamo backed locks multiple processes running on different machines to coordinate work. This provides a different type of coordination than multiple workers consuming from a single queue and can allow quickly prototyping a solution where workers attempt to grab a job and take a lock on it, grabbing another job if they fail at getting the lock. One benefit of this type of solution is that it allows running on spot hardware in the cloud since if a single job is dropped before it is completed the lock will soon expire and another worker will be able to grab that same piece of work.

Much like the locks provided by the threading/multiprocessing modules, these can (and probably should) be used as context managers.

Examples

SocketLock

SocketLock requires no additional third party libs, and should work well on *nix OS's.

Advantages:

  • if a process dies the lock is released ~instantly
  • no lockfiles polluting the filesystem

Disadvantages:

  • requires all processes to be on the same host OS
  • only works on nix-based os's (and maybe not even mac)
from locking import SocketLock
import time

with SocketLock(lockname="uniquename") as mylock:
    # at this point we're holding the lock and can
    # safely perform operations without worrying about
    # other threads/process holding a lock with this name
    # interfering
    time.sleep(1)

FileLock

FileLock requires no additional third party libs and should work on most OS's, with the disclaimer that I only have access to *nix OS's.

from locking import FileLock
import time

with FileLock(lockname="foolock") as mylock:
    time.sleep(1)

RedisLock

RedisLock requires redis and obviously a redis server. The advantage of RedisLock over SocketLock or FileLock is that you don't need to be on the same host as other processes. This can be useful if you want one of N hosts to perform some action.

from locking import RedisLock
import time

with RedisLock(lockname="some_process_identifier", hosts=["myredis.com"]):
    time.sleep(1)

DynamoLock

DynamoLock doesn't require an always on redis like RedisLock, however it does require dynamodb access on AWS. In theory this should be pretty cheap.

from locking import DynamoLock
import time

with DynamoLock(lockname="some_process_identifier", table="locks", checkpoint_frequency=2, ttl=5):
    time.sleep(1)

Installation

From PyPI

python -m pip install --upgrade locking

From GitHub

python -m pip install --upgrade git+https://git@github.com/jbylund/locking.git

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

locking-1.1.6.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

locking-1.1.6-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file locking-1.1.6.tar.gz.

File metadata

  • Download URL: locking-1.1.6.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for locking-1.1.6.tar.gz
Algorithm Hash digest
SHA256 82954caf5d5b4e10b3dc578d6c5c11ff6c070e5e6444f1e192f72f70b1b3bd83
MD5 1a21090a9ef35cb2df6a03bcddf10fd1
BLAKE2b-256 6312aa4656ec95fa5f39125c44f5423e7c69eb3ab2ae043cc6991219f43f66f4

See more details on using hashes here.

File details

Details for the file locking-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: locking-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for locking-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0ec066907e58ed6816f2ec324ca5402cfe90dfb787e41f160e556c6b6838678c
MD5 d32f09242a80f44c728161790e502b0b
BLAKE2b-256 8e4adc3c4f80d448237bb483be0d1e7c1c5d307d781ef104dad72e6a2716a05f

See more details on using hashes here.

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