Pidfile
This class manages process ID (PID) lock files to ensure exclusive access or track running processes. It provides mechanisms to acquire, release, and validate locks, including the ability to detect and remove stale pidfiles if the associated process is no longer active. The class supports the context manager pattern for automated lock lifecycle management.
Attributes
| Attribute | Type | Description |
|---|---|---|
| path | string = null | Path to the pid lock file. |
Constructor
Signature
def Pidfile(
path: string
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| path | string | The file system path where the pid lock file will be created. |
Methods
acquire()
@classmethod
def acquire() - > [Pidfile](pidfile.md?sid=celery_platforms_pidfile)
Acquire lock.
Returns
| Type | Description |
|---|---|
[Pidfile](pidfile.md?sid=celery_platforms_pidfile) | The current Pidfile instance after successfully writing the PID to the lock file |
is_locked()
@classmethod
def is_locked() - > boolean
Return true if the pid lock exists.
Returns
| Type | Description |
|---|---|
boolean | True if the file at the specified path exists, indicating an active lock |
release()
@classmethod
def release(
*args: any
) - > null
Release lock.
Parameters
| Name | Type | Description |
|---|---|---|
| *args | any | Positional arguments typically passed by the context manager exit method |
Returns
| Type | Description |
|---|---|
null |
read_pid()
@classmethod
def read_pid() - > integer
Read and return the current pid.
Returns
| Type | Description |
|---|---|
integer | The process ID stored within the pidfile |
remove()
@classmethod
def remove() - > null
Remove the lock.
Returns
| Type | Description |
|---|---|
null |
remove_if_stale()
@classmethod
def remove_if_stale() - > boolean
Remove the lock if the process isn't running. I.e. process does not respond to signal.
Returns
| Type | Description |
|---|---|
boolean | True if the pidfile was removed because it was stale or invalid, False if the process is still active |
write_pid()
@classmethod
def write_pid() - > null
Writes the current process ID to the lock file and verifies the content through a re-read.
Returns
| Type | Description |
|---|---|
null |