This page provides a detailed technical reference for the RudderStack Python SDK's primary interface. It covers the Client class, the global singleton proxy, and the lifecycle management of event data from method call to internal queuing.
The SDK is designed around a layered architecture where the top layer consists of a public API (the Client) that validates and enqueues events. These events are then processed by a background Consumer thread for batched delivery to the RudderStack Data Plane.
The following diagram illustrates how the top-level module functions interact with the underlying Client singleton and its configuration.
Interface to Entity Mapping
Sources: rudderstack/analytics/client.py21-37 rudderstack/analytics/client.py216-224
The Client class is the engine of the SDK. It manages the internal state, the message queue, and the lifecycle of consumer threads.
The Client is initialized with a variety of parameters, many of which default to values defined in DefaultConfig.
| Parameter | Default | Description |
|---|---|---|
write_key | None | Required. The RudderStack project write key. |
host | TEST_DATA_PLANE_URL | The Data Plane URL (formerly dataPlaneUrl). |
sync_mode | False | If True, events are sent synchronously (blocking). |
max_queue_size | 10000 | Maximum number of unsent messages in the queue. |
upload_size | 100 | Number of messages to include in a single batch. |
upload_interval | 0.5 | Seconds to wait before uploading a partial batch. |
thread | 1 | Number of consumer threads to spawn. |
Sources: rudderstack/analytics/client.py22-36 rudderstack/analytics/client.py40-54
While the internal variable is named host, it represents the dataPlaneUrl. The SDK ensures trailing slashes are removed before appending the /v1/batch endpoint rudderstack/analytics/request.py21
The SDK supports six primary event types. Each method validates input types using the require helper rudderstack/analytics/client.py273-281 and passes a constructed dictionary to the internal _enqueue logic.
Used to associate a user with their traits.
context object rudderstack/analytics/client.py101user_id or anonymous_id must be provided rudderstack/analytics/client.py103Records actions performed by a user.
event (string) and either user_id or anonymous_id rudderstack/analytics/client.py125-127page records website page views, while screen records mobile app screen views.
name and category strings rudderstack/analytics/client.py195-198group_id rudderstack/analytics/client.py169previous_id and user_id rudderstack/analytics/client.py147-148Sources: rudderstack/analytics/client.py96-117 rudderstack/analytics/client.py119-141 rudderstack/analytics/client.py143-160 rudderstack/analytics/client.py162-184 rudderstack/analytics/client.py186-214
_enqueue)Every event call ends in _enqueue(msg). This method handles:
datetime.now() rudderstack/analytics/client.py221-222messageId if missing rudderstack/analytics/client.py226-227utils.clean() rudderstack/analytics/client.py231sync_mode, it calls post() immediately rudderstack/analytics/client.py234-235put_nowait() into the queue.Queue rudderstack/analytics/client.py239Sources: rudderstack/analytics/client.py216-250 rudderstack/analytics/consumer.py56-75
The SDK provides mechanisms to ensure data is sent before the application terminates.
The flush() method blocks until the internal queue is empty. It calls queue.join() to wait for all tasks marked as task_done() by the consumer rudderstack/analytics/client.py252-256
join(): Iterates through all consumer threads and calls consumer.pause() followed by consumer.join() rudderstack/analytics/client.py258-264shutdown(): A convenience method that first calls flush() and then join() to ensure a clean exit rudderstack/analytics/client.py266-271self.join with the Python atexit handler to prevent messy shutdowns when the interpreter exits, though this does not guarantee a full flush rudderstack/analytics/client.py81The on_error callback can be provided to the constructor. If the Consumer encounters a non-retryable error or exhausts retries, it executes this callback with the exception and the failed batch rudderstack/analytics/consumer.py69-70
Sources: rudderstack/analytics/client.py252-271 rudderstack/analytics/consumer.py63-75 rudderstack/analytics/client.py74-81
Refresh this wiki