Skip to content

Commit

Permalink
Add option to call services in a separate thread (#847)
Browse files Browse the repository at this point in the history
* Register call_service operation to run in its own thread

* Configure call_service behavior with node parameter
  • Loading branch information
sea-bass committed Mar 8, 2023
1 parent f9f8d21 commit a5c395f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import fnmatch
from functools import partial
from threading import Thread

from rosbridge_library.capability import Capability
from rosbridge_library.internal.services import ServiceCaller
Expand All @@ -52,7 +53,19 @@ def __init__(self, protocol):
Capability.__init__(self, protocol)

# Register the operations that this capability provides
protocol.register_operation("call_service", self.call_service)
call_services_in_new_thread = protocol.node_handle.get_parameter(
"call_services_in_new_thread"
).get_parameter_value()
if call_services_in_new_thread:
# Calls the service in a separate thread so multiple services can be processed simultaneously.
protocol.node_handle.get_logger().info("Calling services in new thread")
protocol.register_operation(
"call_service", lambda msg: Thread(target=self.call_service, args=(msg,)).start()
)
else:
# Calls the service in this thread, so services block and must be processed sequentially.
protocol.node_handle.get_logger().info("Calling services in existing thread")
protocol.register_operation("call_service", self.call_service)

def call_service(self, message):
# Pull out the ID
Expand Down
3 changes: 3 additions & 0 deletions rosbridge_server/launch/rosbridge_websocket_launch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<arg name="unregister_timeout" default="10.0" />

<arg name="use_compression" default="false" />
<arg name="call_services_in_new_thread" default="false" />

<arg name="topics_glob" default="" />
<arg name="services_glob" default="" />
Expand All @@ -33,6 +34,7 @@
<param name="max_message_size" value="$(var max_message_size)"/>
<param name="unregister_timeout" value="$(var unregister_timeout)"/>
<param name="use_compression" value="$(var use_compression)"/>
<param name="call_services_in_new_thread" value="$(var call_services_in_new_thread)"/>

<param name="topics_glob" value="$(var topics_glob)"/>
<param name="services_glob" value="$(var services_glob)"/>
Expand All @@ -49,6 +51,7 @@
<param name="max_message_size" value="$(var max_message_size)"/>
<param name="unregister_timeout" value="$(var unregister_timeout)"/>
<param name="use_compression" value="$(var use_compression)"/>
<param name="call_services_in_new_thread" value="$(var call_services_in_new_thread)"/>

<param name="topics_glob" value="$(var topics_glob)"/>
<param name="services_glob" value="$(var services_glob)"/>
Expand Down
4 changes: 4 additions & 0 deletions rosbridge_server/scripts/rosbridge_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ def __init__(self):
def protocol_parameter_handling(self):
RosbridgeWebSocket.use_compression = self.declare_parameter("use_compression", False).value

RosbridgeWebSocket.call_services_in_new_thread = self.declare_parameter(
"call_services_in_new_thread", False
).value

# get RosbridgeProtocol parameters
RosbridgeWebSocket.fragment_timeout = self.declare_parameter(
"fragment_timeout", RosbridgeWebSocket.fragment_timeout
Expand Down

0 comments on commit a5c395f

Please sign in to comment.