| 1 |
# Copyright (C) 2006, Nuxeo SAS <http://www.nuxeo.com> |
|---|
| 2 |
# |
|---|
| 3 |
# This library is free software; you can redistribute it and/or |
|---|
| 4 |
# modify it under the terms of the GNU Lesser General Public |
|---|
| 5 |
# License as published by the Free Software Foundation; either |
|---|
| 6 |
# version 2.1 of the License, or (at your option) any later version. |
|---|
| 7 |
# |
|---|
| 8 |
# This library is distributed in the hope that it will be useful, |
|---|
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 11 |
# Lesser General Public License for more details. |
|---|
| 12 |
# |
|---|
| 13 |
# You should have received a copy of the GNU Lesser General Public |
|---|
| 14 |
# License along with this library; if not, write to the Free Software |
|---|
| 15 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-13 |
|---|
| 16 |
"""Python File Lock. |
|---|
| 17 |
|
|---|
| 18 |
$Id$ |
|---|
| 19 |
""" |
|---|
| 20 |
|
|---|
| 21 |
import os |
|---|
| 22 |
import time |
|---|
| 23 |
|
|---|
| 24 |
class PythonFileLock(object): |
|---|
| 25 |
|
|---|
| 26 |
# XXX this lock has some problems in a multi threading environement. |
|---|
| 27 |
# XXX this lock has a serious preformance issue. |
|---|
| 28 |
|
|---|
| 29 |
# This implementation is not used yet by the PythonFileDirectory. |
|---|
| 30 |
# See nxlucene.store.lock.py using |
|---|
| 31 |
|
|---|
| 32 |
LOCK_POLL_INTERVAL = 1000 |
|---|
| 33 |
|
|---|
| 34 |
def __init__(self, lockDir, lockFile): |
|---|
| 35 |
self.name = lockFile |
|---|
| 36 |
self.lockDir = lockDir |
|---|
| 37 |
self.lockFile = os.path.join(lockDir, lockFile) |
|---|
| 38 |
|
|---|
| 39 |
def isLocked(self): |
|---|
| 40 |
return os.path.exists(self.lockFile) |
|---|
| 41 |
|
|---|
| 42 |
def obtainTimeout( self, timeout ): |
|---|
| 43 |
locked = self.obtain() |
|---|
| 44 |
maxSleepCount = round(timeout / self.LOCK_POLL_INTERVAL) |
|---|
| 45 |
sleepCount = 0 |
|---|
| 46 |
while (not locked): |
|---|
| 47 |
if sleepCount >= maxSleepCount: |
|---|
| 48 |
raise Exception("Lock obtain timed out: " + self.toString()) |
|---|
| 49 |
time.sleep(timeout/1000) |
|---|
| 50 |
locked = self.obtain() |
|---|
| 51 |
sleepCount += 1 |
|---|
| 52 |
return locked |
|---|
| 53 |
|
|---|
| 54 |
def obtain(self): |
|---|
| 55 |
if not os.path.exists(self.lockDir): |
|---|
| 56 |
os.makedirs(self.lockDir) |
|---|
| 57 |
|
|---|
| 58 |
if self.isLocked(): |
|---|
| 59 |
return False |
|---|
| 60 |
|
|---|
| 61 |
try: |
|---|
| 62 |
open(self.lockFile, 'w') |
|---|
| 63 |
except: |
|---|
| 64 |
return False |
|---|
| 65 |
else: |
|---|
| 66 |
return True |
|---|
| 67 |
|
|---|
| 68 |
def release(self): |
|---|
| 69 |
try: |
|---|
| 70 |
os.remove(self.lockFile) |
|---|
| 71 |
except OSError: |
|---|
| 72 |
return False |
|---|
| 73 |
else: |
|---|
| 74 |
return True |
|---|
| 75 |
|
|---|
| 76 |
def toString(self): |
|---|
| 77 |
return 'Lock@' + self.lockFile |
|---|