Skip to main content

A lightweight cross-platform single-instance process lock for Python.

Project description

yyds-lock

PyPI version License: MIT

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-lock binds 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 (using msvcrt.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.
  • 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

  1. Linux / macOS: Uses fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) for exclusive advisory locking.
  2. Windows: Uses msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) to lock the first byte of the file.
  3. 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.
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yyds_lock-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

yyds_lock-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

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

Hashes for yyds_lock-0.2.0.tar.gz
Algorithm Hash digest
SHA256 aaa24fbf76e11fec017f3f14c128992cf75995f0cb92c7fa9975a9ae83e615b0
MD5 507cad25dc0c82f752593f0e38d87fbc
BLAKE2b-256 9986c7dfcb6995d759318203a535baa144fee9e66ab10d08a77a44f1d55333e6

See more details on using hashes here.

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

Hashes for yyds_lock-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 453b5b91ba0c1b356983d41b9c1823aad92127bcd83ef5e5a14cb9e2cd12f49c
MD5 fbacad2b62c4119ad5b80e103c42e32b
BLAKE2b-256 be2ed63c5889abb2334f68dee016162a3a63b37932a7d6377870f1aac7e83e3d

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