Skip to content

Commit

Permalink
implement timeout() def
Browse files Browse the repository at this point in the history
  • Loading branch information
guits committed Nov 26, 2015
1 parent 274b1e9 commit 387c02f
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions jsondb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

import json
import logging
import signal
import errno
import fcntl
from contextlib import contextmanager
import time


@contextmanager
def timeout(seconds):
def timeout_handler(signum, frame):
pass

original_handler = signal.signal(signal.SIGALRM, timeout_handler)

try:
signal.alarm(seconds)
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, original_handler)
def timeout(timeout=3, start=None):
now = int(time.time())
if (now - start) >= 3:
return True
#logger.DEBUG('Lock timed out!')
return False


class JsonDB(object):
Expand All @@ -39,15 +31,18 @@ def __exit__(self, type, value, traceback):
self._release()

def _lock(self):
with timeout(self._timeout):
self._f = open(self._dbfile, 'w+')
self._start = int(time.time())
while True:
try:
fcntl.flock(self._f.fileno(), fcntl.LOCK_EX)
self._f = open(self._dbfile, 'w+')
fcntl.flock(self._f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
self.load()
except IOError as e:
if e.errno != errno.EINTR:
raise e
self._logger.DEBUG('Lock timed out!')
time.sleep(0.01)
if timeout(timeout=self._timeout, start=self._start):
return True

def _release(self):
self._f.close()
Expand Down

0 comments on commit 387c02f

Please sign in to comment.