tencent cloud

All product documents
TDMQ for Apache Pulsar
SDK for Python
Last updated: 2024-06-28 11:33:56
SDK for Python
Last updated: 2024-06-28 11:33:56

Overview

This document describes how to use open-source SDK to send and receive messages by using the SDK for Python as an example and helps you better understand the message sending and receiving processes.

Prerequisites

You have created or prepared the required resources as instructed in Resource Creation and Preparation.
You have installed Python. For the download address, click here.
You have installed pip. For the download address, click here.
You have downloaded the demo. For the download address, click here.

Directions

1. Prepare the environment.
Install the pulsar-client library in the client environment as instructed in Pulsar Python client.
pip install 'pulsar-client==3.1.0'
2. Create a client.
# Create a client
client = pulsar.Client(
authentication=pulsar.AuthenticationToken(
# Authorized role token
AUTHENTICATION),
# Service access address
service_url=SERVICE_URL)
Parameter
Description
SERVICE_URL
Cluster access address, which can be viewed and copied on the Cluster page in the console.
Loading…

AUTHENTICATION
Role token, which can be copied in the Token column on the Role Management page.
Loading…

3. Create a producer.
# Create a producer
producer = client.create_producer(
# Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`, which can be copied from the **Topic** page.
topic='pulsar-xxx/sdk_python/topic1'
)
Note:
You need to enter the complete path of the topic name, that is, persistent://clusterid/namespace/Topic, where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.
4. Send the message.
# Send the message
producer.send(
# Message content
'Hello python client, this is a msg.'.encode('utf-8'),
# Message parameter
properties={'k': 'v'},
# Business key
partition_key='yourKey'
)
The message can also be sent in async mode.
# Send the callback in async mode
def send_callback(send_result, msg_id):
print('Message published: result:{} msg_id:{}'.format(send_result, msg_id))

# Send the message
producer.send_async(
# Message content
'Hello python client, this is a async msg.'.encode('utf-8'),
# Async callback
callback=send_callback,
# Message configuration
properties={'k': 'v'},
# Business key
partition_key='yourKey'
)
5. Create a consumer.
# Subscribe to the message
consumer = client.subscribe(
# Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`, which can be copied from the **Topic** page.
topic='pulsar-xxx/sdk_python/topic1',
# Subscription name
subscription_name='sub_topic1'
)
Note:
You need to enter the complete path of the topic name, that is, persistent://clusterid/namespace/Topic, where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.

You need to enter the subscription name for the subscriptionName parameter. The name can be viewed on the Consumption Management page.
6. Consume the message.
# Obtain the message
msg = consumer.receive()
try:
# Simulate the business processing logic
print("Received message '{}' id='{}'".format(msg.data(), msg.message_id()))
# Return `ack` as the acknowledgement if the consumption is successful
consumer.acknowledge(msg)
except:
# If the consumption fails, the message will be delivered again.
consumer.negative_acknowledge(msg)
7. Log in to the TDMQ for Apache Pulsar console, click Topic > Topic Name to enter the consumption management page, and click the triangle below a subscription name to view the production and consumption records.



Note
Above is a brief introduction to message publishing and subscription. For more information, see Demo or Pulsar Python client.

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

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support