Cron Lock

The fussy.cronlock module provides a simple mechanism to prevent runaway cron jobs. A lock can be used to protect a piece of code that should only ever have a single running instance:

from fussy import cronlock
lock = cronlock.Lock( 'test.lock' )

with lock:
    do_something()

It can also be used to prevent a piece of code from running for too long (note: the lock does not need to be held for this to work):

from fussy import cronlock
lock = cronlock.Lock( 'test.lock' )

lock.set_timeout( 20 ) # from this moment
do_something_that_might_take_a_while()

A decorator is provided to create and hold the lock for a given function, normally the main function of your cron-lock:

from fussy import with_lock

@with_lock( 'test.lock', timeout=20 )
def main():
    """Your cron job main-loop"""

Module: fussy.cronlock

Provide a cron lock for preventing cron-bombs and the like

exception fussy.cronlock.Busy[source]

Raised if the lock is held by another cronlock

__weakref__

list of weak references to the object (if defined)

class fussy.cronlock.Flock(filename)[source]

A context manager that just flocks a file

__weakref__

list of weak references to the object (if defined)

class fussy.cronlock.Lock(lockfile, quiet_fail=False)[source]

A Context manager that provides cron-style locking

__init__(lockfile, quiet_fail=False)[source]

Create a lock file

name – used to construct the lock-file name directory – directory in which to construct the lock-file

__weakref__

list of weak references to the object (if defined)

set_timeout(duration)[source]

Set a signal to fire after duration and raise an error

exception fussy.cronlock.Timeout[source]

Raised if the timeout signal triggers

__weakref__

list of weak references to the object (if defined)

fussy.cronlock.with_lock(name, directory=None, timeout=None)[source]

Decorator that runs a function with Lock instance acquired

  • name – basename of the file to create

  • directory – if specified, the directory in which to store files,

    defaults to tempfile.gettempdir()

  • timeout – if specified, the number of seconds to allow before raising

    a Timeout error

Project Versions

Table Of Contents

Previous topic

Tree Linking

Next topic

South Rollbacks

This Page