0
$\begingroup$

Are there any recommended ways for doing full multithreaded processing (multiple cores) with ROS2 python/rclpy? The multithreaded executor is often recommended, but it seems that executor is subject to the global interpreter lock (GIL).

In my research it seems some people might have tried using the multiprocessing library but were unable to get it to work (causes deadlock?)

$\endgroup$

1 Answer 1

2
$\begingroup$

The multithreaded executor is often recommended, but it seems that executor is subject to the global interpreter lock (GIL).

Yes, looking at the implementation of rclpy.MultiThreadedExecutor in the latest ROS2 LTS release (Humble), it's built on concurrent.futures.ThreadPoolExecutor, which is itself built on Python threads, therefore it's subject to the GIL.

One alternative that has recently become available is to use a free-threading version of Python. On Ubuntu you can set it up like this:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update 
sudo apt install python3.13-nogil python3.13-venv
python3.13-nogil -m venv --system-site-packages ros2_nogil
source ros2_nogil/bin/activate

...but I haven't tried it myself yet.

Another possibility is to use the multiprocessing module to run your computations on multiple processes, but keeping the ROS2 callbacks in a single thread. For example, have a pair of multiprocessing.Queue objects, one sending incoming messages to a process pool for handling, another receiving outgoing messages for publishing. That way the ROS message callbacks are freed from processing duties, increasing their potential throughput.

$\endgroup$
1
  • 1
    $\begingroup$ I think this is the right approach, but it would be great if someone created some good examples. I suppose the community is focused on C++ right now when most performance/concurrency is a concern. $\endgroup$ Commented Sep 16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.