https://github.com/pykit3/k3portlock
k3protlock is a cross-process lock that is implemented with `tcp` port binding.
https://github.com/pykit3/k3portlock
lock port python
Last synced: 4 months ago
JSON representation
k3protlock is a cross-process lock that is implemented with `tcp` port binding.
- Host: GitHub
- URL: https://github.com/pykit3/k3portlock
- Owner: pykit3
- License: mit
- Created: 2021-07-26T08:05:03.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2025-08-28T12:37:53.000Z (9 months ago)
- Last Synced: 2025-09-29T21:59:57.308Z (8 months ago)
- Topics: lock, port, python
- Language: Python
- Homepage: https://blog.openacid.com
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# k3portlock
[](https://github.com/pykit3/k3portlock/actions/workflows/python-package.yml)
[](https://k3portlock.readthedocs.io/en/stable/?badge=stable)
[](https://pypi.org/project/k3portlock)
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
```python
#!/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 (张炎泼)
# Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼)
[pykit3]: https://github.com/pykit3