Rev | Line | |
---|
[976] | 1 | ###
|
---|
| 2 | ### backmon.lib.locking
|
---|
| 3 | ###
|
---|
| 4 | import os
|
---|
| 5 | import os.path
|
---|
| 6 | import time
|
---|
| 7 |
|
---|
| 8 | from .exceptions import BackmonError
|
---|
| 9 |
|
---|
| 10 | #
|
---|
| 11 | # lock() - Lock a file using lock directory semantics
|
---|
| 12 | #
|
---|
| 13 | def lock(path, retries=1, sleep=1):
|
---|
| 14 |
|
---|
| 15 | lock_dir = '%s.lock' % (path)
|
---|
| 16 |
|
---|
| 17 | for i in range(retries):
|
---|
| 18 | try:
|
---|
| 19 | os.mkdir(lock_dir)
|
---|
| 20 | return True
|
---|
| 21 | except OSError:
|
---|
| 22 | time.sleep(sleep)
|
---|
| 23 |
|
---|
| 24 | return False
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | #
|
---|
| 28 | # unlock() - Unlock a file using lock directory semantics
|
---|
| 29 | #
|
---|
| 30 | def unlock(path, retries=1, sleep=1):
|
---|
| 31 |
|
---|
| 32 | lock_dir = '%s.lock' % (path)
|
---|
| 33 |
|
---|
| 34 | for i in range(retries):
|
---|
| 35 | try:
|
---|
| 36 | os.rmdir(lock_dir)
|
---|
| 37 | return True
|
---|
| 38 | except OSError:
|
---|
| 39 | time.sleep(sleep)
|
---|
| 40 |
|
---|
| 41 | return False
|
---|
| 42 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.