Skip to main content

MySQL Backed Locking Primitive

Project description

py-mysql-lock

MySQL Backed Locking Primitive

https://img.shields.io/pypi/v/PyMySQLLock.svg https://api.travis-ci.com/sanketplus/py-mysql-lock.svg Coverage! Documentation Status

py-mysql-lock provides locking primitive based on MySQL’s GET_LOCK

Use Cases

Though there are mature locking primitives provided by systems like Zookeeper and etcd, when you have an application which is primarily dependent on MySQL for its uptime and health, added resiliency provided by systems just mentioned doesn’t add much benefit. py-mysql-lock helps when you have multiple application instances which are backed by a common mysql instance and you want only one of those application instances to hold a lock and do certain tasks.

Documentation: https://py-mysql-lock.readthedocs.io

Installation

py-mysql-lock can be installed from pip. Alternate installation methods can be found in installation documentation

pip install PyMySQLLock

Usage

py-mysql-lock works with existing python mysql libraries like PyMYSQL, mysql-connector-python amd mysqlclient.

Getting A Named Lock

Getting a named lock is a three step process. First you will create a Locker instance. Argument to locker are the same arguments that you give to your MySQL library’s connect method. Locker then can be used to create Locks. Locks can be acquired and released.

from PyMySQLLock import Locker

locker = Locker(host="localhost", user="root",
                password="password", database="db")
lock = locker.lock("lock_name")

lock.acquire()  # returns True if lock is acquired

# do something

lock.release()

Timeout For Acquisition

Call to a lock’s acquire method takes an optional timeout arguments. The value is timeout in seconds. Default value is -1 which denotes wait for indefinite time.

# wait for 10 seconds. If lock is not acquired, False is returned
lock.acquire(timeout=10)

Refresh Interval

Since the lock is acquired on a mysql connection, it is important for the connection to stay alive. To prevent connection from breaking due to inactivity, py-mysql-lock keeps pinging on the connection in background. The default ping interval is 10 seconds and optionally can be set with acquire call,

lock.acquire(timeout=10, refresh_interval_secs=1)

Get All Locks

You can also query the mysql database for getting all current acquired locks on the given db.

locker.get_all_locks()  # returns list of names (string) of locks acquired on the db

Credits: This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.2.0 (2020-09-13)

  • Add API to get all acquired locks on given mysql db

0.1.0 (2020-09-13)

  • Add background lock connection refresh

0.0.1 (2020-09-12)

  • First release on PyPI.

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

PyMySQLLock-0.2.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

PyMySQLLock-0.2.0-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file PyMySQLLock-0.2.0.tar.gz.

File metadata

  • Download URL: PyMySQLLock-0.2.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for PyMySQLLock-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5271b6a6f11ac8603272bb14c27c0c5935930019ed2359b58cb70804277da88f
MD5 fe42b7684761db4072583e544c18c785
BLAKE2b-256 aa7168882e5c3601e3f2f62d53736e397338f58c8254b9419a21d7aa15a6a281

See more details on using hashes here.

File details

Details for the file PyMySQLLock-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: PyMySQLLock-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for PyMySQLLock-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5568d4789e383d51f17a0d40ccdc8f2fe23590f09d8210b1e05e40cc0ea1b315
MD5 f366806ca798ff90320f030fc309d2a6
BLAKE2b-256 f244926aa02f24cecf2b8561b9191eb2db11b89aa21989f0b13c764b39992ca4

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