@@ -38,41 +38,26 @@ def lock_file(filename, content, operation):
38
38
return handle
39
39
40
40
41
- def unlock_file (filename , handle , remove = True ):
42
- """Unlock given file."""
43
- fcntl .flock (handle , fcntl .LOCK_UN )
44
- handle .close ()
45
- if remove :
46
- try :
47
- os .unlink (filename )
48
- except OSError :
49
- pass
41
+
42
+ def try_remove (filename ):
43
+ try :
44
+ os .unlink (filename )
45
+ except OSError :
46
+ pass
50
47
51
48
52
49
@contextlib .contextmanager
53
- def file_lock (filename , operation = fcntl . LOCK_EX , remove = True , timeout = 20 ):
50
+ def file_lock (filename , remove = True , timeout = 20 ):
54
51
"""A lock that is shared across processes.
55
52
56
53
:param filename: the name of the file that will be locked.
57
- :pram operation: the lock operation.
58
54
59
55
"""
60
- # http://bservices_log.vmfarms.com/2011/03/cross-process-locking-and.html
61
- times = 0
62
- while True :
63
- try :
64
- handle = lock_file (filename , None , operation )
65
- break
66
- except IOError :
67
- if times > timeout :
68
- raise Exception ('Not able to aquire lock file {0} in {1}' .format (filename , timeout ))
69
- time .sleep (0.5 )
70
- times += 1
56
+ with contextlib .closing (zc .lockfile .LockFile (filename )) as lockfile :
57
+ yield lockfile ._fp
71
58
72
- try :
73
- yield handle
74
- finally :
75
- unlock_file (filename , handle , remove = remove )
59
+
60
+ remove and try_remove (filename )
76
61
77
62
78
63
def unlock_resource (name , resource , lock_dir , services_log ):
0 commit comments