A lightweight cross-platform single-instance process lock for Python.
Project description
yyds-lock
yyds-lock is an ultra-lightweight, zero-dependency Python library that guarantees single-instance execution of scripts/processes using operating system level advisory file locks. It is ideal for cron jobs, automation scripts, schedulers, and background daemons.
Key Features
- 🛡️ Immunity to Crashes / Force Kills: Unlike simple PID files or "lock files" that leave stale markers behind if the script crashes, is killed (
kill -9), or suffers power loss,yyds-lockbinds the lock to the process file descriptor. The OS automatically and instantly releases the lock as soon as the process ends. - 🪶 Zero Dependencies: 100% pure Python standard library. The installation size is less than 5KB and does not pollute your environment.
- 🎛️ Dual Modes: Supports both "Instant Exit" (non-blocking, terminates immediately if another instance is running) and "Queue / Wait" (blocking, waits for the existing instance to finish).
- 💻 Cross-Platform: Seamlessly works on Linux, macOS (using
fcntl.flock), and Windows (usingmsvcrt.locking).
Installation
pip install -U yyds-lock
Usage
You can protect your script using either of two simple approaches:
Pattern A: Direct Call (Best for straightforward scripts / entrypoints)
Place this call at the very top of your entrypoint script. If another instance of the script is already running, the new instance will immediately print an error to stderr and exit with status code 1.
import time
import yyds_lock
# Force single-instance execution.
yyds_lock.force_single(lock_name="my_automation.lock", block=False)
print("Running heavy automation task...")
time.sleep(300)
Pattern B: Decorator (Best for structured functions/main entrypoints)
Decorate your main function to enforce mutual exclusion.
import yyds_lock
@yyds_lock.single_decorator(lock_name="my_task.lock", block=False)
def main():
print("Executing single instance task safely...")
if __name__ == "__main__":
main()
Configuration / Arguments
Both force_single and single_decorator accept the following arguments:
lock_name(str): The filename/path of the lock.- If a simple filename is given (e.g.
"my_job.lock"), it is automatically created in the user's home directory (~). - If an absolute or relative path is given (e.g.,
"/var/run/my_job.lock"), it is created at that specific path. The parent directories will be created automatically if they do not exist.
- If a simple filename is given (e.g.
block(bool):False(default): Exit immediately if the lock cannot be acquired.True: Block and queue, waiting for the active process to finish and release the lock.
How It Works Under the Hood
- Linux / macOS: Uses
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)for exclusive advisory locking. - Windows: Uses
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)to lock the first byte of the file. - The library stores the open file handles in a global dictionary inside the Python runtime. This keeps the file descriptor open and prevents garbage collection (GC) from releasing the lock prematurely.
- When the process terminates (normally, via Exception,
sys.exit, crash,kill -9, or power failure), the OS closes the file descriptors, releasing the locks instantly.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yyds_lock-0.2.0.tar.gz.
File metadata
- Download URL: yyds_lock-0.2.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaa24fbf76e11fec017f3f14c128992cf75995f0cb92c7fa9975a9ae83e615b0
|
|
| MD5 |
507cad25dc0c82f752593f0e38d87fbc
|
|
| BLAKE2b-256 |
9986c7dfcb6995d759318203a535baa144fee9e66ab10d08a77a44f1d55333e6
|
File details
Details for the file yyds_lock-0.2.0-py3-none-any.whl.
File metadata
- Download URL: yyds_lock-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
453b5b91ba0c1b356983d41b9c1823aad92127bcd83ef5e5a14cb9e2cd12f49c
|
|
| MD5 |
fbacad2b62c4119ad5b80e103c42e32b
|
|
| BLAKE2b-256 |
be2ed63c5889abb2334f68dee016162a3a63b37932a7d6377870f1aac7e83e3d
|