~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/lockfile.py

  • Committer: Robey Pointer
  • Date: 2007-01-02 07:35:34 UTC
  • Revision ID: robey@lag.net-20070102073534-u7tkc8q07xsz9wlv
fewer todos

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
with_lock = util.with_lock('_tlock', 'LockFile')
27
27
 
28
 
MAX_STALE_TIME = 5 * 60
29
 
 
30
28
 
31
29
class LockFile (object):
32
30
    """
35
33
    lock may be acquired multiple times by the same thread, as long as it's
36
34
    released an equal number of times.  unlike threading.Lock, this lock can
37
35
    be used across processes.
38
 
 
 
36
    
39
37
    this uses os.open(O_CREAT|O_EXCL), which apparently works even on windows,
40
38
    but will not work over NFS, if anyone still uses that.  so don't put the
41
39
    cache folder on an NFS server...
46
44
        # thread lock to maintain internal consistency
47
45
        self._tlock = threading.Lock()
48
46
        self._count = 0
49
 
        if os.path.exists(filename):
50
 
            # remove stale locks left over from a previous run
51
 
            if time.time() - os.stat(filename).st_mtime > MAX_STALE_TIME:
52
 
                os.remove(filename)
53
 
 
 
47
    
54
48
    @with_lock
55
49
    def _try_acquire(self):
56
50
        if self._count > 0:
63
57
            return True
64
58
        except OSError:
65
59
            return False
66
 
 
 
60
    
67
61
    def acquire(self):
68
62
        # try over and over, sleeping on exponential backoff with an upper limit of about 5 seconds
69
63
        pause = 0.1