From 1598c6b427c73d2fb87cbea5325c08068556dc61 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Tue, 3 Sep 2024 21:39:02 -0700 Subject: [PATCH] High-Latency networks: "generator already executing" when calling a service ref: https://github.com/ros2/rclpy/issues/1351 Signed-off-by: Tomoya Fujita --- prover_rclpy/setup.py | 1 + prover_rclpy/src/rclpy_1351.py | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 prover_rclpy/src/rclpy_1351.py diff --git a/prover_rclpy/setup.py b/prover_rclpy/setup.py index 567230c..201b914 100644 --- a/prover_rclpy/setup.py +++ b/prover_rclpy/setup.py @@ -79,6 +79,7 @@ 'rclpy_1273 = src.rclpy_1273:main', 'rclpy_1287 = src.rclpy_1287:main', 'rclpy_1303 = src.rclpy_1303:main', + 'rclpy_1351 = src.rclpy_1351:main', 'ros2cli_818 = src.ros2cli_818:main', 'ros2cli_862 = src.ros2cli_862:main', 'ros2cli_885 = src.ros2cli_885:main', diff --git a/prover_rclpy/src/rclpy_1351.py b/prover_rclpy/src/rclpy_1351.py new file mode 100644 index 0000000..c8d36ce --- /dev/null +++ b/prover_rclpy/src/rclpy_1351.py @@ -0,0 +1,37 @@ +import threading +import time + +import rclpy +from rclpy.executors import Executor +from rclpy.executors import MultiThreadedExecutor +from rclpy.executors import SingleThreadedExecutor +from rclpy.task import Future + + +def main(args=None): + rclpy.init(args=args) + executor = SingleThreadedExecutor() + + future1 = Future(executor=executor) + future2 = Future(executor=executor) + + # I believe that we need to use ThreadPoolExecutor here... + thread1 = threading.Thread(target=executor.spin_until_future_complete, + args=(future1, 10)) + thread2 = threading.Thread(target=executor.spin_until_future_complete, + args=(future2, 10)) + + thread1.start() + time.sleep(0.5) + thread2.start() + + future1.set_result(True) + future2.set_result(True) + + thread1.join() + thread2.join() + executor.shutdown() + + +if __name__ == '__main__': + main()