Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CARLA simulator Carla Client Constructor

From Leeroopedia
Knowledge Sources
Domains Autonomous Driving Simulation, Distributed Systems
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for establishing a TCP connection from a Python client to a running CARLA simulation server provided by the CARLA simulator.

Description

The Client constructor creates a new client instance that connects to the CARLA server process via msgpack-rpc over TCP. Upon construction, the client establishes the underlying network connection to the specified host and port. The optional worker_threads parameter controls the size of the internal thread pool used for asynchronous I/O operations; a value of 0 lets the implementation choose a sensible default.

After construction, the client object serves as the primary entry point for all server-level operations such as loading maps, retrieving the simulation world, setting timeouts, and querying server metadata. The client also exposes the Traffic Manager port configuration used for autopilot behavior.

Usage

Use the Client constructor at the very beginning of any CARLA script or application. It is the mandatory first step before any simulation interaction can occur. Typical usage includes connecting to a locally running server on the default port, or connecting to a remote server by specifying its IP address.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/client/Client.h
  • Lines: L29-32
  • Python binding: PythonAPI/carla/src/Client.cpp
  • Lines: L189-191

Signature

Client(host: str, port: int, worker_threads: int = 0) -> Client

Import

import carla

I/O Contract

Inputs

Name Type Required Description
host str Yes Hostname or IP address of the machine running the CARLA server (e.g., "localhost", "192.168.1.100")
port int Yes TCP port number the CARLA server is listening on (default server port is 2000)
worker_threads int No Number of worker threads for the internal async I/O pool. Defaults to 0, which lets the implementation select an appropriate value.

Outputs

Name Type Description
return carla.Client A connected client instance ready to issue commands to the CARLA server

Usage Examples

Basic Example

import carla

# Connect to a locally running CARLA server on the default port
client = carla.Client("localhost", 2000)

# Set a timeout for all subsequent server calls (in seconds)
client.set_timeout(10.0)

# Verify the connection by querying the server version
print(f"Connected to CARLA server version: {client.get_server_version()}")
print(f"Client API version: {client.get_client_version()}")

# Retrieve the simulation world
world = client.get_world()
print(f"Current map: {world.get_map().name}")

Remote Server Example

import carla

# Connect to a CARLA server running on a remote machine with extra worker threads
client = carla.Client("192.168.1.50", 2000, worker_threads=4)
client.set_timeout(30.0)

# Check version compatibility
server_version = client.get_server_version()
client_version = client.get_client_version()
if server_version != client_version:
    print(f"Warning: version mismatch - server={server_version}, client={client_version}")

world = client.get_world()

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment