Skip to content

Commit

Permalink
Add max_retries param to AsyncWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
RouganStriker committed Jan 29, 2020
1 parent 3cac626 commit 1484f85
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/whoosh/writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,20 +972,23 @@ class AsyncWriter(threading.Thread, IndexWriter):
>>> writer = AsyncWriter(myindex)
"""

def __init__(self, index, delay=0.25, writerargs=None):
def __init__(self, index, delay=0.25, max_retries=0, writerargs=None):
"""
:param index: the :class:`whoosh.index.Index` to write to.
:param delay: the delay (in seconds) between attempts to instantiate
the actual writer.
:param writerargs: an optional dictionary specifying keyword arguments
to to be passed to the index's ``writer()`` method.
:param max_retries: the maximum number of attempts to instantiate the
actual writer. If the value is 0, retry indefinitely.
"""

threading.Thread.__init__(self)
self.running = False
self.index = index
self.writerargs = writerargs or {}
self.delay = delay
self.max_retries = max_retries
self.events = []
try:
self.writer = self.index.writer(**self.writerargs)
Expand All @@ -1008,10 +1011,17 @@ def _record(self, method, args, kwargs):
def run(self):
self.running = True
writer = self.writer
while writer is None:
retry_attempts = 0
while writer is None and self.max_retries > 0 and retry_attempts <= self.max_retries:
try:
writer = self.index.writer(**self.writerargs)
except LockError:
retry_attempts += 1

if retry_attempts > self.max_retries:
raise LockError("Failed to acquire index lock after "
"{max_retries} attempts.".format(max_retries=self.max_retries))

time.sleep(self.delay)
for method, args, kwargs in self.events:
getattr(writer, method)(*args, **kwargs)
Expand Down

0 comments on commit 1484f85

Please sign in to comment.