-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1178 from MetPX/issue1173
Fix #1173: problems stopping instances
- Loading branch information
Showing
10 changed files
with
161 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Long running sleeps prevent Sarracenia from being shutdown cleanly in a | ||
reasonable amount of time. | ||
This implements a reusable sleep function that can be called from other parts | ||
of the code to sleep for a long time, but can still be interrupted. | ||
""" | ||
|
||
import time | ||
|
||
def interruptible_sleep(sleep_time:float, obj: object, stop_flag_name: str='_stop_requested', nap_time: float=5.0) -> bool: | ||
""" Sleep for sleep_time, divided up into shorter nap_time intervals. | ||
Pass a reference to an object that contains a boolean attribute named stop_flag_name. | ||
Between each nap_time, the function will check if obj.stop_flag_name has become True. | ||
If the flag is False, it will continue sleeping, if True, it will abort the sleep. | ||
Args: | ||
sleep_time (float): total amount of time to sleep, if not interrupted | ||
obj (object): the object containing the boolean attribute named stop_flag_name | ||
stop_flag_name (str): the name of the boolean attribute in obj | ||
nap_time (float): default = 5.0, sleep in intervals of nap_time | ||
Returns: | ||
bool: ``True`` if the sleep **was** interrupted, ``False`` if it slept for the entire ``sleep_time`` | ||
time without interruption. | ||
""" | ||
interrupted = False | ||
while sleep_time > 0 and not interrupted: | ||
time.sleep(min(nap_time, sleep_time)) | ||
sleep_time -= nap_time | ||
interrupted = (hasattr(obj, stop_flag_name) and getattr(obj, stop_flag_name)) | ||
return interrupted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.