The `pid` library (current version 3.0.4) provides robust pidfile management capabilities, including stale detection and file-locking. It can be used as a context manager or a decorator to ensure only one instance of a process is running, and to handle cleanup of the pidfile on termination. It's a stable library with a focus on reliable process control.
pip install pidVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates using `PidFile` as a context manager to ensure a single instance of a process is running. It attempts to create a pidfile, performs some simulated work, and automatically cleans up the pidfile upon exiting the `with` block. It also shows how to catch common exceptions if another instance is detected.
Ensure your code relies on the process environment being determined at the point of lock acquisition/check, not instance creation, especially in forking processes. Re-test daemonized applications if upgrading from <2.0.0.
Be aware that `pid` installs its own `SIGTERM` handler for cleanup. If your application has its own `SIGTERM` handler, ensure it correctly integrates or is compatible with `pid`'s cleanup mechanism, or that your custom handler explicitly calls functions registered with `atexit`.
When handling exceptions, consider catching `PidFileAlreadyLockedError` first if you expect concurrent access to the pidfile. Catching `PidFileError` (the base exception) will capture both `PidFileAlreadyLockedError` and `PidFileAlreadyRunningError` if you want a more general catch-all.
This is often not an error but an informative message. If you want to allow multiple instances, the `pid` library might not be suitable or requires custom handling. To stop the existing instance, terminate the process with the reported PID (e.g., `kill 12345`).
This usually indicates a stale pidfile or a mismanaged process. Verify if the process with the reported PID is indeed your application. If it's a legitimate active process, handle it as `PidFileAlreadyLockedError`. If it's a stale pidfile, `pid` should ideally clean it up if configured, but manual intervention (deleting the `.pid` file) might be necessary if cleanup failed.
Specify a writable directory for the pidfile using the `piddir` argument, e.g., `PidFile(pidname='my_app_daemon', piddir='/tmp')`. Alternatively, ensure the application is run with sufficient privileges or that the target directory has appropriate permissions.
Install the library using pip: `pip install pid`.
No dependency data recorded yet.