Skip to content

Sleep falls out quickly when shutting down #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions python_dynamodb_lock/python_dynamodb_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,16 @@ def _start_heartbeat_sender_thread(self):
self._heartbeat_sender_thread.daemon = True
self._heartbeat_sender_thread.start()
logger.info('Started the heartbeat-sender thread: %s', str(self._heartbeat_sender_thread))


def _sleep_with_break_out(self, total_time_to_sleep):
"""
Allows the sleep to be interrupted if shutting down
"""
while (total_time_to_sleep > 0):
time.sleep(1)
total_time_to_sleep = total_time_to_sleep - 1
if self._shutting_down:
break

def _send_heartbeat_loop(self):
"""
Expand Down Expand Up @@ -157,14 +166,14 @@ def _send_heartbeat_loop(self):
curr_loop_end_time = time.monotonic()
next_loop_start_time = start_time + count * avg_loop_time
if curr_loop_end_time < next_loop_start_time:
time.sleep( next_loop_start_time - curr_loop_end_time )
self._sleep_with_break_out(next_loop_start_time - curr_loop_end_time)

# After all the locks have been "heartbeat"-ed, sleep before the next run (if needed)
logger.info('Finished the send_heartbeat loop')
end_time = time.monotonic()
next_start_time = start_time + self._heartbeat_period.total_seconds()
if end_time < next_start_time and not self._shutting_down:
time.sleep( next_start_time - end_time )
self._sleep_with_break_out(next_start_time - end_time)
elif end_time > next_start_time + avg_loop_time:
logger.warning('Sending heartbeats for all the locks took longer than the _heartbeat_period')

Expand Down Expand Up @@ -281,7 +290,7 @@ def _check_heartbeat_loop(self):
end_time = time.monotonic()
next_start_time = start_time + self._heartbeat_period.total_seconds()
if end_time < next_start_time and not self._shutting_down:
time.sleep( next_start_time - end_time )
self._sleep_with_break_out(next_start_time - end_time)
else:
logger.warning('Checking heartbeats for all the locks took longer than the _heartbeat_period')

Expand Down Expand Up @@ -481,7 +490,7 @@ def acquire_lock(self,
)
elif next_loop_start_time > curr_loop_end_time:
logger.info('Sleeping before a retry: %s', new_lock.unique_identifier)
time.sleep(next_loop_start_time - curr_loop_end_time)
self._sleep_with_break_out(next_loop_start_time - curr_loop_end_time)


def release_lock(self, lock, best_effort=True):
Expand Down