Skip to content

Commit 85c0395

Browse files
committed
Use lockfile library instead of custom implementation
1 parent 66fe5d4 commit 85c0395

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'cherrypy',
3232
'bottle',
3333
'clize',
34+
'lockfile',
3435
],
3536
include_package_data=True,
3637
dependency_links=[

zerobin/paste.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import hashlib
77
import base64
8+
import lockfile
89

910
from datetime import datetime, timedelta
1011

@@ -124,18 +125,9 @@ def increment_counter(self):
124125
"""
125126
path = settings.PASTE_FILES_ROOT
126127
counter_file = os.path.join(path, 'counter')
127-
lock_file = os.path.join(path, 'counter.lock')
128-
129-
# TODO : change lock implementation to use the lockfile lib
130-
# https://pypi.python.org/pypi/lockfile
131-
# The current lock implementation sucks. It skips some increment, and
132-
# still allows race conditions.
133-
if not os.path.isfile(lock_file):
134-
try:
135-
# Aquire lock file
136-
with open(lock_file, "w") as flock:
137-
flock.write('lock')
128+
lock = lockfile.LockFile(counter_file)
138129

130+
with lock:
139131
# Read the value from the counter
140132
try:
141133
with open(counter_file, "r") as fcounter:
@@ -147,9 +139,6 @@ def increment_counter(self):
147139
with open(counter_file, "w") as fcounter:
148140
fcounter.write(str(counter_value))
149141

150-
finally:
151-
# remove lock file
152-
os.remove(lock_file)
153142

154143
def save(self):
155144
"""

0 commit comments

Comments
 (0)