简体中文界面。帖子、指南及政策正文保留原文;未对原文作自动翻译。

机器人对比

Python or C++ for a robot application?

Choose Python, C++ or a mixed robot application from measured latency, supported interfaces and maintenance needs, with an explicit boundary around real-time control.

内容概要

Choose the language for the external application's actual workload and supported interfaces. Python can suit coordination, experiments and data workflows; C++ may suit a measured computational or integration requirement. A mixed application can be sensible. Neither language alone provides deterministic timing, a compatible robot driver or a validated safety function.

Illustration of a teach pendant, backup drive and industrial cables beside a cobot
Application Robot editorial illustration, AI-generated; not a product photograph or installation drawing

Locate the code in the robot system

Begin by stating where this software runs and what authority it has. An external PC application selecting a job is different from a controller programme executing a path, and both differ from a device's servo control. This guide concerns the external application. A language decision should not silently move motion ownership or protective functions from an established controller into an ordinary desktop process.

Draw the path from observation to decision to controller request. Identify the interfaces, units and state transitions at each boundary. If the robot already accepts complete validated jobs, the external programme may spend most of its time waiting for results. If it processes camera data before every decision, computation and data movement may dominate. Those distinctions tell you what to measure before debating Python versus C++.

参考资料: ROS 2 Jazzy client libraries, official documentation source · Understanding real-time programming, ROS 2 Jazzy documentation source

Classify the workload before choosing a language

The same application can contain configuration screens, image processing, log analysis and robot communications. They need not share one implementation strategy. Use the table to identify the evidence that should decide each component. The entries are engineering starting points, not benchmarks. Team experience and the available vendor interface may outweigh a modest difference in measured execution time.

Check whether a required library already performs expensive work in native code. Calling such a library from Python does not mean all its computation executes as Python bytecode. Conversely, a C++ wrapper around a slow service does not remove that service's delay. Profile the complete path and preserve the input data so alternative implementations are compared on the same task.

WorkloadReason to consider PythonReason to consider C++
Job coordinationReadable integration with existing Python toolsExisting supported C++ SDK and maintainers
Experiment analysisAvailable analysis workflow and rapid iterationA specific library or measured processing bottleneck
Image or numerical pipelineEfficient library bindings already availableRequired native API or proven latency advantage
ROS 2 componentSupported rclpy implementation fits the jobSupported rclcpp implementation fits the job
Deadline-sensitive pathOnly with demonstrated system-level suitabilityMore implementation control, still requiring timing evidence

参考资料: ROS 2 Jazzy client libraries, official documentation source · threading: Thread-based parallelism, Python 3.13 documentation · Understanding real-time programming, ROS 2 Jazzy documentation source

Let supported interfaces constrain the shortlist

Build a compatibility record before writing the application: robot model, controller firmware, SDK release, operating system, runtime or compiler, required libraries and deployment architecture. Ask whether the SDK provides the same functions and error information in both languages. A tutorial showing a connection is not evidence that a binding supports cancellation, restart and diagnostics for your intended controller build.

The official ROS 2 Jazzy documentation identifies rclpy for Python and rclcpp for C++ and explains their shared client-library foundation. It also notes language-specific execution behaviour. That supports interoperability, not the assumption that every API or scheduling detail is interchangeable. This guide uses Jazzy as a named documentation baseline. It does not assert that an arbitrary Python 3.13 environment is a supported binary installation for that ROS distribution.

参考资料: ROS 2 Jazzy client libraries, official documentation source

Understand which Python runtime you are evaluating

For the standard GIL-enabled CPython 3.13 build, the threading documentation explains the limit on simultaneous execution of Python code in threads and the usefulness of threading for suitable waiting or I/O workloads. That distinction matters when an application combines socket waits with computation. Measure how much time is actually spent in Python execution, native libraries and waiting before deciding that adding threads will solve the bottleneck.

Python 3.13 also has an experimental free-threaded build. Its official guide explains that extension compatibility matters and that an imported extension can cause the GIL to be enabled again. Therefore Python version alone is not a complete concurrency description. Record the build and relevant dependencies. Do not change runtime variants in a robot deployment merely to pursue theoretical parallelism without checking support, behaviour and reproducibility.

参考资料: threading: Thread-based parallelism, Python 3.13 documentation · Python experimental support for free threading, Python 3.13 documentation

Distinguish fast execution from meeting deadlines

An average response time is not a deadline guarantee. A process can usually finish quickly yet occasionally wait on scheduling, allocation, a lock or an external dependency. The ROS 2 Jazzy real-time tutorial discusses deadlines and avoiding nondeterministic work in critical paths. Its demonstration has its own platform assumptions; this guide uses the conceptual explanation and does not reproduce its system-tuning or execution instructions.

C++ provides mechanisms useful for controlling resource use, but a C++ application can still allocate unpredictably, block on an unbounded wait or depend on an overloaded operating system. Define the required timing boundary, allowable variation and response to a missed deadline with the system designer. Keep safety-related control within an appropriately designed and validated architecture. A language label cannot provide the evidence required for that architecture.

参考资料: Understanding real-time programming, ROS 2 Jazzy documentation source

Worked hypothetical example: optimise the measured stage

Suppose a simulated inspection workflow has a hypothetical 500-millisecond deadline from receiving an image to producing a usable decision. A representative serial trace assigns 40 milliseconds to preparation, 180 to inference, 20 to decision logic and 60 to transport and waiting: 300 milliseconds total. These invented values illustrate a budget, not measured performance of Python, C++, ROS or a robot.

Rewriting only the 20-millisecond decision stage so it takes 5 milliseconds reduces the illustrated total to 285 milliseconds, a five-percent improvement. It may not be worth the new build and maintenance work. If preparation or transport has occasional long delays, investigate those distributions separately. Preserve failures and the slow observations. The right decision can be a smaller data copy, a changed queue policy or a bounded request flow rather than a language rewrite.

参考资料: Understanding real-time programming, ROS 2 Jazzy documentation source · threading: Thread-based parallelism, Python 3.13 documentation

Use a mixed application where the boundary is clear

A Python coordinator and a C++ processing component can exchange a defined result instead of sharing internal objects. ROS 2's client-library documentation explicitly describes communication between nodes written with different libraries. In another architecture, a vendor-supported interface may provide the boundary. Choose the arrangement that makes the data, failure and lifecycle contracts understandable to the team.

Define result identity, units, frame, timestamp meaning and freshness requirements. Decide whether a busy consumer queues, rejects or replaces pending work, and ensure the producer can distinguish those outcomes. A boundary introduces serialization, scheduling and deployment work, so measure it rather than assuming separation is free. Keep both sides versioned against the same interface tests and make it possible to reproduce a failed exchange without moving physical equipment.

参考资料: ROS 2 Jazzy client libraries, official documentation source

Compare the cost of diagnosing and changing the application

Choose a small representative slice that includes the real SDK, data sizes and error paths. Exercise it with recorded or simulated inputs, inspect the logs and compare the timing requirements. Ask another maintainer to change a rule, reproduce a fault and rebuild or recreate the environment. Those tasks reveal support costs that a short algorithm benchmark will not show. Include deployment updates and rollback in the decision.

Write the result as a bounded engineering choice: selected language per component, supported version set, measured constraint, and reason the rejected option adds no necessary benefit. Keep unmeasured assumptions visible. Reopen the decision when an SDK loses support or a workload crosses the demonstrated performance boundary. For Application Robot, label all simulation-derived timings and keep any later physical validation under the responsible integration team's control.

参考资料: ROS 2 Jazzy client libraries, official documentation source · Understanding real-time programming, ROS 2 Jazzy documentation source

检查清单

  • Identify external application responsibilities and retained controller ownership.
  • Record the supported SDK, runtime or compiler and dependency baseline.
  • Profile computation, waiting and data movement separately.
  • Specify deadline boundaries and retain slow or failed attempts.
  • Record CPython build and extension compatibility when assessing threading.
  • Define message identity, units, freshness and overload behaviour across components.
  • Compare maintenance and deployment work before committing to a rewrite.

常见问题

Is Python too slow to control a robot application?

That question needs a workload and interface boundary. Python may be sufficient for job coordination or library-backed processing, while another component may need a different implementation. Measure the relevant path and respect the controller's supported interface and timing requirements.

参考资料: threading: Thread-based parallelism, Python 3.13 documentation · ROS 2 Jazzy client libraries, official documentation source

Does rewriting in C++ make a system real-time?

No. Deadline behaviour depends on execution paths, scheduling, memory, synchronization and external dependencies. C++ may help implement a suitable design, but the complete system still needs timing analysis and validation appropriate to its role.

参考资料: Understanding real-time programming, ROS 2 Jazzy documentation source

Can Python and C++ nodes work together in ROS 2?

Yes. The Jazzy client-library documentation describes communication across language implementations using ROS interfaces. Define the message contract and test the selected versions; shared middleware does not make application-specific timing or error handling automatic.

参考资料: ROS 2 Jazzy client libraries, official documentation source

来源与审核

Documentary language-selection guidance checked on 6 September 2026. Sources are explicitly scoped to ROS 2 Jazzy and Python 3.13, including its experimental free-threaded build; no cross-version compatibility is assumed. Timing values are hypothetical, not benchmarks or robot-control guarantees.

适合读者:Robotics developers choosing an external application language. 更新于 .

  1. ROS 2 Jazzy client libraries, official documentation sourceROS 2 · 核查于
  2. Understanding real-time programming, ROS 2 Jazzy documentation sourceROS 2 · 核查于
  3. threading: Thread-based parallelism, Python 3.13 documentationPython Software Foundation · 核查于
  4. Python experimental support for free threading, Python 3.13 documentationPython Software Foundation · 核查于
编辑方针 · 提交勘误