k3portlock
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.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| k3portlock-0.1.9.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| k3portlock-0.1.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.4 kB
Release files / k3portlock-0.1.9.tar.gz
| Download URL | k3portlock-0.1.9.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4db41c60425c8aa3f0133638ee439ef7ba933753f2c6efaa62abed5ae9be692f
|
|
BLAKE2b-256 checksum How to use checksums |
e279bef906ad2458fb04d257bd2e5c42fbf666f89bd1f7beb186a251c118d8d6
|
| 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.9-py3-none-any.whl
| Download URL | k3portlock-0.1.9-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8e9fd44808a63ce9064051b414f1adcf2453b9f8aefb89c5d69f735fd107e41f
|
|
BLAKE2b-256 checksum How to use checksums |
3adcf18ac0a6041e206255435b9409e2e3b296c53ddf9d1918c159bc4d69ac99
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|