Skip to main content

k3portlock

Action-CI Documentation Status Package

k3portlock is a cross-process lock that is implemented with socket binding.

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

k3portlock is a cross-process lock that is implemented with socket binding. No two sockets can bind the same address, so whoever binds the address of a key holds the lock of that key.

On Linux, the address is the abstract Unix socket /portlock/<key>. Only processes in the same network namespace share these locks. A container with its own network does not see the locks of the host.

On other systems, k3portlock hashes the key to one of 20000 base ports in [40000, 60000). k3portlock tries to bind 3 ports from the base port 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. Two different keys whose base ports are less than 3 apart can block each other, and so can other programs that bind these ports. With 100 keys in use at once, about one pair of keys is that close. A collision looks like a lock held by another process. try_lock() returns False, and acquire() raises PortlockTimeout unless the other lock is released in time.

A lock lives as long as its socket. release() or the exit of the process closes the socket and frees the lock, so a killed process never leaves a stale 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

Metadata

Release files for k3portlock 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for k3portlock 0.1.10
File Size Uploaded
k3portlock-0.1.10.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for k3portlock 0.1.10
File Interpreter ABI Platform
k3portlock-0.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / k3portlock-0.1.10.tar.gz

Download URL k3portlock-0.1.10.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
273576d95cf9eac0c177b034260b4218f205e3230a90136db0d46867f8071c5a
BLAKE2b-256 checksum
How to use checksums
2aacefa363b2c896d9b15761820bb0ba473e8f6619e9f371f7b7f05b983361a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / k3portlock-0.1.10-py3-none-any.whl

Download URL k3portlock-0.1.10-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
64641c471f6adc4134c3d725559f8e2d2c36264e6d30c40b404087627c9d43ad
BLAKE2b-256 checksum
How to use checksums
14099c8db461dcab65c109d8462d3a9ac5ad3e42e2c2485b3324b727be57e881
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release history Release notifications | RSS feed

0.1.11

2 release files

This release

0.1.10 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page