Skip to main content

Distributed lock manager, support many types of backend, e.g. redis, django-redis, etcd, zookeeper...

Project description

globallock

Distributed lock manager, support many types of backend, e.g. redis, django-redis, etcd, zookeeper...

Install

pip install globallock

Notice: there're two l in globallock.

RedisGlobalLock Usage

from globallock import GlobalLockManager
from globallock import REDIS_GLOBAL_LOCK_CLASS
# RedisGlobalLock is the default engine class,
# so config item global_lock_engine_class is optional here
config = {
    "global_lock_engine_class": REDIS_GLOBAL_LOCK_CLASS, # optional
    "global_lock_engine_options": {
        # redis connection pool init options goes here...
        "host": "xxx",
        "port": 6379,
        "password": "xxx",
        "db": 0,
    }
}
lockman = GlobalLockManager(config)
lockname = "your unique lock name"
with lockman.lock(lockname, timeout=60, blocking=True, blocking_timeout=5) as lock:
    if lock.is_locked:
        pass # do something with lock...
    else:
        pass # do something without lock, mostly do NOTHING...

DjangoRedisGlobalLock Usage

from globallock import GlobalLockManager
from globallock import DJANGO_REDIS_GLOBAL_LOCK_CLASS
config = {
    "global_lock_engine_class": DJANGO_REDIS_GLOBAL_LOCK_CLASS, # required
    "global_lock_engine_options": {
        "redis-cache-name": "default", # redis-cache-name is default to `default`
    }
}
lockman = GlobalLockManager(config)
lockname = "your unique lock name"
with lockman.lock(lockname, timeout=60, blocking=True, blocking_timeout=5) as lock:
    if lock.is_locked:
        pass # do something with lock...
    else:
        pass # do something without lock, mostly do NOTHING...

EtcdGlobalLock Usage

from globallock import GlobalLockManager
from globallock import ETCD_GLOBAL_LOCK_CLASS
config = {
    "global_lock_engine_class": ETCD_GLOBAL_LOCK_CLASS, # required
    "global_lock_engine_options": { 
        # etcd3 client init options goes here...
    }
}
lockman = GlobalLockManager(config)
lockname = "your unique lock name"
with lockman.lock(lockname, timeout=60, blocking=True, blocking_timeout=5) as lock:
    if lock.is_locked:
        pass # do something with lock...
    else:
        pass # do something without lock, mostly do NOTHING...

ZookeeperGlobalLock Usage

from globallock import GlobalLockManager
from globallock import ZOOKEEPER_GLOBAL_LOCK_CLASS
config = {
    "global_lock_engine_class": ZOOKEEPER_GLOBAL_LOCK_CLASS, # required
    "global_lock_engine_options": { 
        # KazooClient init options goes here...
    }
}
lockman = GlobalLockManager(config)
lockname = "your unique lock name"
with lockman.lock(lockname, blocking=True, blocking_timeout=5) as lock:
    if lock.is_locked:
        pass # do something with lock...
    else:
        pass # do something without lock, mostly do NOTHING...

Notice:

  • timeout parameter for lockman.lock() will not work for ZookeeperGlobalLock.
  • With ZookeeperGlobalLock, if the process which ownned the lock kill without any signal(kill -9), other process can acquire the lock after a short time(about 10 seconds after the lock-owner process killed). With other global lock engine, you have to wait the lock's timeout effect, after the the lock-owner process killed.

Engine Class Requirements

  • RedisGlobalLock:
    • pip install redis
  • DjangRedisGlobalLock:
    • pip install django-redis
  • EtcdGlobalLock:
    • You need to download the source code of etcd3 from https://github.com/kragniz/python-etcd3, and install it with shell command (pip install .). You can NOT install it via pip install etcd3. The latest version of etcd3 installed via pip is 0.12.0, but it can not work with it's requires packages of latest version.
    • Of course, if the etcd3 package published in pypi upgraded, you can try to install it via pip command.
    • Before the etcd3 projects goes on and release new package to fix the problems, you can pip install zencore-etcd3 instead. zencore-etcd3 is just pull the latest source code of etcd3 form https://github.com/kragniz/python-etcd3, build it as pypi package and upload it to the pypi site.
  • ZookeeperGlobalLock:
    • pip install kazoo

Notice: The packages above are not added to the package's requirements.txt, so you need to install them by yourself, or put them into your projects' requirements.txt.

Releases

v0.1.0

  • First release.
  • Add RedisGlobalLock implementations.
  • Add DjangoRedisGlobalLock implementations.
  • Add EtcdGlobalLock implementations.
  • Add ZookeeperGlobalLock implementations.

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

globallock-0.1.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

globallock-0.1.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file globallock-0.1.0.tar.gz.

File metadata

  • Download URL: globallock-0.1.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for globallock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 62415be178e145f479ed36fe608c7c97a25bcb65f4e3e1eec253979226e2cb47
MD5 7d9f6404b14c7db540f7832626a0dc39
BLAKE2b-256 91abcc38f80c73b28cfbc5931e10d96f864b6cdf7aa20867cbf6a436656972be

See more details on using hashes here.

File details

Details for the file globallock-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: globallock-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for globallock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50e5d6010cbb7f2f1df3847cb021eacd0d5419a408ca0dacc488c150f82235f2
MD5 765ab9086f6972f5b8b194fa119d0db8
BLAKE2b-256 9ff1c39c7b9ae91cb778d0252c53317cdd5b44919e289e21bd0c700398837578

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