Skip to content

Commit e0d8371

Browse files
committed
Use event instead of condition variable for simplicity
1 parent ad7332b commit e0d8371

File tree

1 file changed

+12
-15
lines changed
  • rosbridge_library/src/rosbridge_library/internal

1 file changed

+12
-15
lines changed

rosbridge_library/src/rosbridge_library/internal/services.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3131
# POSSIBILITY OF SUCH DAMAGE.
3232

33-
from threading import Condition, Thread
33+
from threading import Event, Thread
3434
from typing import Any, Callable, Optional
3535

3636
from rclpy.callback_groups import ReentrantCallbackGroup
@@ -155,30 +155,27 @@ def call_service(
155155
raise InvalidServiceException(service)
156156

157157
future = client.call_async(inst)
158-
condition = Condition()
158+
event = Event()
159159

160160
def future_done_callback():
161-
with condition:
162-
condition.notify_all()
161+
event.set()
163162

164-
future.add_done_callback(lambda future: future_done_callback())
163+
future.add_done_callback(lambda _: future_done_callback())
165164

166-
with condition:
167-
if not condition.wait_for(
168-
lambda: future.done(),
169-
timeout=(server_response_timeout if server_response_timeout > 0 else None),
170-
):
171-
future.cancel()
172-
node_handle.destroy_client(client)
173-
raise Exception("Timeout exceeded while waiting for service response")
165+
if not event.wait(timeout=(server_response_timeout if server_response_timeout > 0 else None)):
166+
future.cancel()
167+
node_handle.destroy_client(client)
168+
raise Exception("Timeout exceeded while waiting for service response")
169+
170+
node_handle.destroy_client(client)
174171

175172
result = future.result()
176173

177-
node_handle.destroy_client(client)
178174
if result is not None:
179175
# Turn the response into JSON and pass to the callback
180176
json_response = extract_values(result)
181177
else:
182-
raise Exception("Service call returned None")
178+
exception = future.exception()
179+
raise Exception("Service call exception: " + str(exception))
183180

184181
return json_response

0 commit comments

Comments
 (0)