-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathasync_tasks.py
More file actions
183 lines (137 loc) · 5.48 KB
/
async_tasks.py
File metadata and controls
183 lines (137 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (c) 2023 Boston Dynamics, Inc. All rights reserved.
#
# Downloading, reproducing, distributing or otherwise using the SDK Software
# is subject to the terms and conditions of the Boston Dynamics Software
# Development Kit License (20191101-BDSDK-SL).
"""Utilities for managing periodic tasks consisting of asynchronous GRPC calls."""
import abc
from bosdyn.util import now_sec
from .exceptions import ResponseError, RpcError
class AsyncTasks(object):
"""Manages a set of tasks which work by periodically calling an update() method.
Args:
tasks: List of tasks to manage.
"""
def __init__(self, tasks=None):
self._tasks = tasks if tasks else []
def add_task(self, task):
"""Add a task to be managed by this object.
Args:
task: Task to add.
"""
self._tasks.append(task)
def update(self):
"""Call this periodically to manage execution of tasks owned by this object."""
for task in self._tasks:
task.update()
# pylint: disable=too-few-public-methods
class AsyncGRPCTask(object, metaclass=abc.ABCMeta):
"""Task to be accomplished using asynchronous GRPC calls.
When it is time to run the task, an async GRPC call is run resulting in a FutureWrapper object.
The FutureWrapper is monitored for completion, and then an action is taken in response.
"""
def __init__(self):
self._last_call = 0
self._future = None
@abc.abstractmethod
def _start_query(self):
"""Override to start async grpc query and return future-wrapper for result."""
@abc.abstractmethod
def _should_query(self, now_sec):
"""Called on update() when no query is running to determine whether to start a new query.
Args:
now_sec: Time now in seconds.
Override to return True when a new query should be started.
"""
@abc.abstractmethod
def _handle_result(self, result):
"""Override to handle result of grpc query when it is available.
Args:
result: Result to handle.
"""
@abc.abstractmethod
def _handle_error(self, exception):
"""Override to handle any exception raised in handling GRPC result.
Args:
exception: Error exception to handle.
"""
def update(self):
"""Call this periodically to manage execution of task represented by this object."""
now_sec = now_sec()
if self._future is not None:
if self._future.original_future.done():
try:
self._handle_result(self._future.result())
except (RpcError, ResponseError) as err:
self._handle_error(err)
self._future = None
elif self._should_query(now_sec):
self._last_call = now_sec
self._future = self._start_query()
# pylint: disable=too-few-public-methods
class AsyncPeriodicGRPCTask(AsyncGRPCTask, metaclass=abc.ABCMeta):
"""Periodic task to be accomplished using asynchronous GRPC calls.
When it is time to run the task, an async GRPC call is run resulting in a FutureWrapper object.
The FutureWrapper is monitored for completion, and then an action is taken in response.
Args:
periodic_sec: Time to wait in seconds between queries.
"""
def __init__(self, period_sec):
super(AsyncPeriodicGRPCTask, self).__init__()
self._period_sec = period_sec
def _should_query(self, now_sec):
"""Check if it is time to query again.
Args:
now_sec: Time now in seconds.
Returns:
True if it is time to query again based on now_sec, False otherwise.
"""
return (now_sec - self._last_call) > self._period_sec
@abc.abstractmethod
def _start_query(self):
"""Override to start async grpc query and return future-wrapper for result."""
@abc.abstractmethod
def _handle_result(self, result):
"""Override to handle result of grpc query when it is available.
Args:
result: Result to handle.
"""
@abc.abstractmethod
def _handle_error(self, exception):
"""Override to handle any exception raised in handling GRPC result.
Args:
exception: Error exception to handle.
"""
class AsyncPeriodicQuery(AsyncPeriodicGRPCTask):
"""Query for robot data at some regular interval.
Args:
query_name: Name of the query.
client: SDK client for the query.
logger: Logger to use for logging errors.
periodic_sec: Time in seconds between running the query.
"""
def __init__(self, query_name, client, logger, period_sec):
super(AsyncPeriodicQuery, self).__init__(period_sec)
self._query_name = query_name
self._client = client
self._logger = logger
self._proto = None
@abc.abstractmethod
def _start_query(self):
"""Override to start async grpc query and return future-wrapper for result."""
@property
def proto(self):
"""Get latest response proto."""
return self._proto
def _handle_result(self, result):
"""Handle result of grpc query when it is available.
Args:
result: Result to handle.
"""
self._proto = result
def _handle_error(self, exception):
"""Log exception.
Args:
exception: Error exception to log.
"""
self._logger.exception("Failure getting %s: %s", self._query_name, exception)