tencent cloud

TDMQ for RocketMQ

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Introduction and Selection of the TDMQ Product Series
What Is TDMQ for RocketMQ
Strengths
Scenarios
Product Series
Comparison with Open-Source RocketMQ
High Availability
Quotas and Limits
Supported Regions
Basic Concepts
Billing
Billing Overview
Pricing
Billing Examples
Pay-as-you-go Switch to Monthly Subscription (5.x)
Renewal
Viewing Consumption Details
Refund
Overdue Payments
Getting Started
Getting Started Guide
Preparations
Step 1: Creating TDMQ for RocketMQ Resources
Step 2: Using the SDK to Send and Receive Messages (Recommended)
Step 2: Running the TDMQ for RocketMQ Client (Optional)
Step 3: Querying Messages
Step 4: Deleting Resources
User Guide
Usage Process Guide
Configuring Account Permissions
Creating the Cluster
Configuring the Namespace
Configuring the Topic
Configuring the Group
Connecting to the Cluster
Managing Messages
Managing the Cluster
Viewing Monitoring Data and Configuring Alarms
Cross-Cluster Message Replication
Use Cases
Naming Conventions for Common Concepts of TDMQ for RocketMQ
RocketMQ Client Use Cases
RocketMQ Performance Load Testing and Capacity Assessment
Access over HTTP
Client Risk Descriptions and Update Guide
Migration Guide for TencentCloud API Operations Related to RocketMQ 4.x Cluster Roles
Migration Guide
Disruptive Migration
Seamless Migration
Developer Guide
Message Types
Message Filtering
Message Retries
POP Consumption Mode (5.x)
Clustering Consumption and Broadcasting Consumption
Subscription Relationship Consistency
Traffic Throttling
​​API Reference(5.x)
History
API Category
Making API Requests
Topic APIs
Consumer Group APIs
Message APIs
Role Authentication APIs
Hitless Migration APIs
Cloud Migration APIs
Cluster APIs
Data Types
Error Codes
​​API Reference(4.x)
SDK Reference
SDK Overview
5.x SDK
4.x SDK
Security and Compliance
Permission Management
CloudAudit
Deletion Protection
FAQs
4.x Instance FAQs
Agreements
TDMQ for RocketMQ Service Level Agreement
Contact Us

Python SDK

PDF
Focus Mode
Font Size
Last updated: 2026-01-23 17:52:24

Scenarios

This document uses the Python SDK as an example to describe how to send and receive messages through an open-source software development kit (SDK), helping you better understand the complete process of sending and receiving messages.

Prerequisites

You have obtained the client connection parameters as instructed in SDK Overview.

Operation Steps

Step 1: Preparing the Environment

Since Rocketmq-client Python is packaged based on rocketmq-client-cpp, you need to install librocketmq first.
Note:
A Python client only supports Linux and macOS operating systems and does not support Windows systems.
When a Python SDK is used, note that the installed Python software version supports the underlying chip architecture (x86 or ARM). For example, if '64bit','ELF' (such as the x86_64 architecture) is used on an ARM-based macOS system, errors occur.
1. Install librocketmq (2.0.0 or later). For a detailed installation guide, see librocketmq Installation.
2. Run the following command to install rocketmq-client-python.
pip install rocketmq-client-python


Step 2: Producing Messages

Create, compile, and run a message production program.
from rocketmq.client import Producer, Message # Initialize the producer and set the producer group information. Use the full group name, for example, rocketmq-xxx|namespace_python%group1. During message production, the client does not need to be re-created. producer = Producer(groupName) # Set the service address. producer.set_name_server_address(nameserver) # Set permissions (role name and token). producer.set_session_credentials( accessKey, # Role token secretKey, # Role name '' ) # Start the producer. producer.start() # Assemble messages. # Concatenate the topic with the namespace. msg = Message(namespace + '%' + topicName) # Set keys. msg.set_keys(TAGS) # Set tags. msg.set_tags(KEYS) # Message content msg.set_body('This is a new message.') # Send synchronous messages. ret = producer.send_sync(msg) print(ret.status, ret.msg_id, ret.offset) # Release resources. producer.shutdown()
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
groupName
Producer group name. You can copy the name from the Group Management page in the console.
4.x virtual cluster/exclusive cluster: Concatenate the namespace name in the format of full namespace name%group name, such as MQ_INSTxxx_aaa%GroupTest.
4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the group name.
nameserver
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
accessKey
Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console.
namespace
Namespace name. You can copy the name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter.
topicName
Topic name. You can copy the name from the Topic Management page in the console.
TAGS
Tags associated with the message.
KEYS
Sets the message business key.
The Python client of the current open-source community has certain defects in producing messages, resulting in unbalanced loads between different queues of the same topic. For details, see Defect Details.

Step 3: Consuming Messages

Create, compile, and run a message consumption program.
import time

from rocketmq.client import PushConsumer, ConsumeStatus


# Message processing callback.
def callback(msg):
# Simulate the business logic.
print('Received message. messageId: ', msg.id, ' body: ', msg.body)
# If consumption is successful, return CONSUME_SUCCESS.
return ConsumeStatus.CONSUME_SUCCESS
# Return message status after successful consumption.
# return ConsumeStatus.RECONSUME_LATER


# Initialize the consumer and set consumer group information.
consumer = PushConsumer(namespace + '%' + groupName)
# Set the service address.
consumer.set_name_server_address(nameserver)
# Set permissions (role name and token).
consumer.set_session_credentials(
accessKey, # Role token.
secretKey, # Role name.
''
)
# Subscribe to a topic.
consumer.subscribe(namespace + '%' + topicName, callback, TAGS)
print(' [Consumer] Waiting for messages.')
# Start the consumer.
consumer.start()

while True:
time.sleep(3600)
# Release resources.
consumer.shutdown()
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
namespace
Namespace name. You can copy the name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter.
groupName
Producer group name. You can copy the name from the Group Management page in the console.
nameserver
Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
accessKey
Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console.
topicName
Topic name. You can copy the name from the Topic Management page in the console.

Step 4: Viewing Message Details

After a message is sent, you will receive a message ID (messageID). You can choose Message Query > General Query in the console to query the recently sent message, including the message details and trace.
Note:
The above is a brief introduction to message publishing and subscription. For details, see Demo or RocketMQ-Client-Python example.


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback