applicationinstance module

This module holds the ApplicationInstance class, used to handle the one application instance mechanism

class applicationinstance.ApplicationInstance(pidFile, autoExit=True, flock=False)[source]

Bases: object

Class used to handle one application instance mechanism.

Parameters:
  • pidFile (str) – full path of file used to save pid and procname

  • autoExit (bool) – automatically call sys.exit if there is an other instance running

  • flock (bool) – use file-locks to make sure only one instance is checking at the same time

busy()[source]

Check if one application with this instance is currently running.

Returns:

True if an other instance is currently running.

Return type:

bool

check(autoExit=False)[source]

Check if the current application is already running

Parameters:

autoExit (bool) – automatically call sys.exit if there is another instance running

Returns:

True if this is the only application instance

Return type:

bool

exitApplication()[source]

Called when the single instance exit (remove pid file)

flockExclusiv()[source]

Create an exclusive advisory file lock named <PID file>.flock to block the creation of a second instance while the first instance is still in the process of starting (but has not yet completely started).

The purpose is to make 1. the check if the PID lock file already exists 2. and the subsequent creation of the PID lock file an atomic operation by using a blocking “flock” file lock on a second file to avoid that two or more processes check for an existing PID lock file, find none and create a new one (so that only the last creator wins).

Dev notes:

buhtz (2023-09): Not sure but just log an ERROR without doing anything else is IMHO not enough. aryoda (2023-12): It seems the purpose of this additional lock file using an exclusive lock is to block the other process to continue until this exclusive lock is released (= serialize execution). Therefore advisory locks are used via fcntl.flock (see: man 2 fcntl)

flockUnlock()[source]

Remove the exclusive lock. Second instance can now continue but should find it self to be obsolete.

readPidFile()[source]

Read the pid and procname from the file

Returns:

tuple of (pid(int), procname(str))

Return type:

tuple

startApplication()[source]

Called when the single instance starts to save its pid