Skip to main content

Cross-process lock using TCP port binding

Project description

k3portlock

Action-CI Documentation Status Package

k3protlock is a cross-process lock that is implemented with tcp port binding.

k3portlock is a component of pykit3 project: a python3 toolkit set.

k3portlock is a cross-process lock that is implemented with tcp port binding. Since no two processes could bind on a same TCP port.

k3portlock tries to bind 3 ports on loopback ip 127.0.0.1. If a Portlock instance succeeds on binding 2 ports out of 3, it is considered this instance has acquired the lock.

Install

pip install k3portlock

Synopsis

#!/usr/bin/env python

import time
import k3portlock

if __name__ == "__main__":

    # Basic lock acquisition and release
    lock = k3portlock.Portlock("mylock")

    # Try to acquire lock (non-blocking)
    if lock.try_lock():
        print("Lock acquired")
        # Do some work
        lock.release()
        print("Lock released")
    else:
        print("Failed to acquire lock")

    # Blocking lock acquisition with timeout
    lock2 = k3portlock.Portlock("mylock2", timeout=5)
    try:
        lock2.acquire()  # Will wait up to 5 seconds
        print("Lock acquired")
        time.sleep(1)
        lock2.release()
        print("Lock released")
    except k3portlock.PortlockTimeout:
        print("Timeout while waiting for lock")

    # Context manager usage (recommended)
    with k3portlock.Portlock("mylock3", timeout=3) as lock:
        print("Lock acquired via context manager")
        # Do some work
        time.sleep(0.5)
    print("Lock automatically released")

    # Check if lock is held (without acquiring)
    lock4 = k3portlock.Portlock("testlock")
    print(lock4.has_locked())  # False

    lock4.acquire()
    another_lock = k3portlock.Portlock("testlock")
    print(another_lock.has_locked())  # False (different instance)
    lock4.release()

Author

Zhang Yanpo (张炎泼) drdr.xp@gmail.com

Copyright and License

The MIT License (MIT)

Copyright (c) 2015 Zhang Yanpo (张炎泼) drdr.xp@gmail.com

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

k3portlock-0.1.7.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

k3portlock-0.1.7-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file k3portlock-0.1.7.tar.gz.

File metadata

  • Download URL: k3portlock-0.1.7.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3portlock-0.1.7.tar.gz
Algorithm Hash digest
SHA256 2841aa60136bee0095bf5490be29bc216d37e2163d6f2782bf1c241132d7bda1
MD5 56f87e4835a4ef4da6676e161c3473a5
BLAKE2b-256 575abd95dc4da5d4eac6aca5c4c695a5e22060664d7b15da94ec9e37978e2716

See more details on using hashes here.

File details

Details for the file k3portlock-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: k3portlock-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3portlock-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 dbc1c5be6bca58e2409d7ac603313507d6881c7ed8658a9d4e8355f767f88ff4
MD5 de0f0d957ade6f2acc2c82beddc259c1
BLAKE2b-256 4b71e5dc7266653b61f59379687ab0f96293866f92be87e7e030312caf23b80f

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