This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#61 Ability to list and cancel current actions
- Loading branch information
Showing
4 changed files
with
144 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
import threading | ||
import time | ||
from queue import Queue, Empty, Full | ||
|
||
__COMMAND_QUEUE = Queue() | ||
|
||
_LOG = logging.getLogger('bubuku.communicates') | ||
|
||
|
||
def sleep_and_operate(controller, timeout: float): | ||
cur_time = time.time() | ||
finish = cur_time + (0.1 if timeout <= 0 else timeout) | ||
while cur_time < finish: | ||
try: | ||
commnad = __COMMAND_QUEUE.get(block=True, timeout=finish - cur_time) | ||
try: | ||
commnad(controller) | ||
except Exception as e: | ||
_LOG.error('Command finished with error', exc_info=e) | ||
except Empty: | ||
pass | ||
cur_time = time.time() | ||
|
||
|
||
def execute_on_controller_thread(function, timeout): | ||
condition = threading.Condition() | ||
result = [None, True] | ||
|
||
def _execute(controller): | ||
with condition: | ||
if result[1]: | ||
try: | ||
result[0] = function(controller) | ||
finally: | ||
condition.notify() | ||
|
||
finish = time.time() + timeout | ||
with condition: | ||
try: | ||
__COMMAND_QUEUE.put(_execute, timeout=timeout) | ||
except Full: | ||
raise TimeoutError('Timeout expired') | ||
if condition.wait(timeout=finish - time.time()): | ||
return result[0] | ||
else: | ||
result[1] = False | ||
raise TimeoutError('Timeout expired') |
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