trbs / pid

Pidfile featuring stale detection and file-locking, can also be used as context-manager or decorator
https://pypi.python.org/pypi/pid/
Apache License 2.0
102 stars 26 forks source link

Check if PidFile is locked without actually locking it #40

Open tejasvi opened 2 years ago

tejasvi commented 2 years ago

I am using PidFileError exception raised while acquiring the lock to check its acquired status. But it interferes with the other attempts to acquire the lock during the check period. Is there a way get the status directly?

def is_locked(filename):
    try:
        with PidFile(filename):
            # Parallel lock acquire attempts fail here
            return False
    except PidFileError:
        return True