tencent cloud

Python SDK
Last updated:2025-12-24 15:07:56
Python SDK
Last updated: 2025-12-24 15:07:56

Scenarios

This document describes how to use an open-source SDK (taking the Python SDK as an example) to send and receive messages, so as to help 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

1. Prepare the environment. Install the pulsar-client library in the client environment. You can use pip or other methods for installation. For more information, see Pulsar Python client.
pip install 'pulsar-client==3.1.0'
2. Create a client.
# Create a client.
client = pulsar.Client(
authentication=pulsar.AuthenticationToken(
# The authorized role token.
AUTHENTICATION),
# The address used to access the service.
service_url=SERVICE_URL)
Parameter
Description
SERVICE_URL
Address used to access the cluster. You can view and copy the address from the Cluster page in the console.
AUTHENTICATION
Role token. You can copy the token from the Role Management page.
3. Create a producer.
# Create a producer.
producer = client.create_producer(
# The full topic path in the format of persistent://cluster (tenant) ID/namespace/topic. You can copy the clusterid/namespace/topic part from the Topic page in the console.
topic='pulsar-xxx/sdk_python/topic1'
)
Note
You need to provide the full path of the topic in the format of persistent://clusterid/namespace/topic. You can copy the clusterid/namespace/topic part from the Topic page in the console.
4. Send a message.
# Send a message.
producer.send(
# The message content.
'Hello python client, this is a msg.'.encode('utf-8'),
# The message parameters.
properties={'k': 'v'},
# The business key.
partition_key='yourKey'
)
Messages can also be sent asynchronously.
# Asynchronous sending callback.
def send_callback(send_result, msg_id):
print('Message published: result:{} msg_id:{}'.format(send_result, msg_id))

# Send a message.
producer.send_async(
# The message content.
'Hello python client, this is a async msg.'.encode('utf-8'),
# The asynchronous callback.
callback=send_callback,
# The message configuration.
properties={'k': 'v'},
# The business key.
partition_key='yourKey'
)
5. Create a consumer.
# Subscribe to messages.
consumer = client.subscribe(
# The full topic path in the format of persistent://cluster (tenant) ID/namespace/topic. You can copy the clusterid/namespace/topic part from the Topic page in the console.
topic='pulsar-xxx/sdk_python/topic1',
# The subscription name.
subscription_name='sub_topic1'
)
Note
You need to provide the full path of the topic in the format of persistent://clusterid/namespace/topic. You can copy the clusterid/namespace/topic part from the Topic page in the console. 
You need to enter the subscription name. You can view the subscription name on the Consumer tab of the topic.
6. Consume messages.
# Obtain messages.
msg = consumer.receive()
try:
# Simulate the business logic.
print("Received message '{}' id='{}'".format(msg.data(), msg.message_id()))
# If the consumption succeeds, reply ack.
consumer.acknowledge(msg)
except:
# If consumption fails, the message will be redelivered.
consumer.negative_acknowledge(msg)
7. Log in to the TDMQ for Apache Pulsar console. On the Topic page, click the topic name to go to the Consumption Management page. Click the dropdown arrow next to the subscription name to view production and consumption records.
img


Note
The above is a brief introduction to the publishing and subscription of messages. For more information, see Demo or Pulsar Official Documentation.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback