-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
client.call blocks the thread #181
Comments
After the |
Hi @minggangw thanks for the bug report. I'll close this since it looks to be working as intended. It blocks forever because there isn't an executor running. An executor must be running to unblock Another detail is The smallest change to get the code working is to replace future = cli.call_async(req)
rclpy.spin_until_future_complete(node, future)
response = future.result() That invokes a global executor which is single-threaded. If the global executor isn't good enough for you (say you'd like callbacks to run in multiple threads), then an executor will need to be started separately. from example_interfaces.srv import AddTwoInts
from rclpy.executors import MultiThreadedExecutor
import rclpy
import threading
def run_executor(executor, node):
executor.add_node(node)
try:
executor.spin()
finally:
executor.shutdown()
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node('minimal_client')
cli = node.create_client(AddTwoInts, 'add_two_ints')
# Run executor that uses a thread pool in another thread so main thread is free to do what it wants
exec = MultiThreadedExecutor(num_threads=4)
thread = threading.Thread(target=run_executor, args=(exec, node), daemon=True)
thread.start()
req = AddTwoInts.Request()
req.a = 41
req.b = 1
while not cli.wait_for_service(timeout_sec=1.0):
node.get_logger().info('service not available, waiting again...')
response = cli.call(req)
node.get_logger().info(
'Result of add_two_ints: for %d + %d = %d' %
(req.a, req.b, response.sum))
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main() Save this as
See also ros2/examples an example of a custom executor and compose multiple nodes in a single process. |
Thanks for your explanation @sloretz . |
Bug report
Required Info:
Steps to reproduce issue
Expected behavior
The client will receive the response from service side.
Actual behavior
The thread gets blocked after the line
cli.call(req)
Additional information
suspicious commit 0c2ee4c
The text was updated successfully, but these errors were encountered: