Skip to main content

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

AttributeTypeDescription
pathstring = nullPath to the pid lock file.

Constructor

Signature

def Pidfile(
path: string
) - > null

Parameters

NameTypeDescription
pathstringThe 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

TypeDescription
[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

TypeDescription
booleanTrue if the file at the specified path exists, indicating an active lock

release()

@classmethod
def release(
*args: any
) - > null

Release lock.

Parameters

NameTypeDescription
*argsanyPositional arguments typically passed by the context manager exit method

Returns

TypeDescription
null

read_pid()

@classmethod
def read_pid() - > integer

Read and return the current pid.

Returns

TypeDescription
integerThe process ID stored within the pidfile

remove()

@classmethod
def remove() - > null

Remove the lock.

Returns

TypeDescription
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

TypeDescription
booleanTrue 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

TypeDescription
null