HWRF  trunk@4391
Classes | Functions | Variables
produtil.locking Namespace Reference

Handles file locking using Python "with" blocks. More...

Detailed Description

Handles file locking using Python "with" blocks.

This module implements a Python with construct that can hold a lock and release it at the end of the "with" block. It also implements a safety feature to allow the program to disable locking, ensuring a fatal exception (LockingDisabled) if anything tries to lock a file. That functionality is connected to the produtil.sigsafety module, which will disable locking if a fatal signal is received.

1 import produtil.locking
2 with produtil.locking.LockFile("some.lockfile"):
3  ... do things while the file is locked...
4 ... the file is now unlocked ...

Classes

class  LockFile
 Automates locking of a lockfile. More...
 
class  LockHeld
 This exception is raised when a LockFile cannot lock a file because another process or thread has locked it already. More...
 
class  LockingDisabled
 This exception is raised when a thread attempts to acquire a lock while Python is exiting according to produtil.sigsafety. More...
 

Functions

def disable_locking ()
 Entirely disables all locking in this module. More...
 

Variables

list __all__ = ['LockingDisabled','disable_locking','LockFile','LockHeld']
 Symbols exported by "from produtil.locking import *".
 
tuple locks = set()
 Part of the internal implementation of this module: the list of existing locks (LockFile objects) that may be held. More...
 
 locks_okay = True
 Part of the internal implementation of this module: if True, locking is allowed, if False, locking is forbidden. More...
 

Function Documentation

def produtil.locking.disable_locking ( )

Entirely disables all locking in this module.

If this is called, any locking attempts will raise LockingDisabled. That exception derives directly from BaseException, which well-written Python code will never catch, hence ensuring a rapid, abnormal exit of the program. This routine should never be called directly: it is only used as part of the implementation of the produtil.sigsafety, to prevent file locking after catching a terminal signal, hence allowing the program to exit as quickly as possible, and present a stack trace to any locations that attempt locking.

Definition at line 36 of file locking.py.

Referenced by produtil.sigsafety.hup_handler(), and produtil.sigsafety.term_handler().

Variable Documentation

produtil.locking.locks = set()

Part of the internal implementation of this module: the list of existing locks (LockFile objects) that may be held.

Definition at line 28 of file locking.py.

produtil.locking.locks_okay = True

Part of the internal implementation of this module: if True, locking is allowed, if False, locking is forbidden.

When this is False, LockingDisabled is raised on any attempt to acquire a lock.

Definition at line 34 of file locking.py.