Context

class rclpy.context.Context

Encapsulates the lifecycle of init and shutdown.

Context objects should not be reused, and are finalized in their destructor. Wraps the rcl_context_t type.

Example:
>>> from rclpy.context import Context
>>> with Context() as context:
>>>     context.ok()
True
destroy()
get_domain_id()

Get domain id of context.

property handle
init(args: List[str] | None = None, *, initialize_logging: bool = True, domain_id: int | None = None)

Initialize ROS communications for a given context.

Parameters:

args – List of command line arguments.

ok()

Check if context hasn’t been shut down.

on_shutdown(callback: Callable[[], None])

Add a callback to be called on shutdown.

shutdown()

Shutdown this context.

try_shutdown()

Shutdown this context, if not already shutdown.