tencent cloud

Feedback

SDK for Python

Last updated: 2022-06-29 11:39:32

    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

    Directions

    1. Prepare the environment.
      Install the pulsar-client library in the client environment as instructed in Pulsar Python client.
      pip install pulsar-client==2.8.1
    2. Create a client.
      # Create a client
      client = pulsar.Client(
      authentication=pulsar.AuthenticationToken(
      # 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.
      img
      AUTHENTICATION Role token, which can be copied in the Token column on the Role Management page.img
    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, i.e., 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, i.e., persistent://clusterid/namespace/Topic, where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.
      img
    • You need to enter the subscription name in the subscriptionName parameter, which can be viewed on the Consumption Management page.
    1. 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)
    2. Log in to the TDMQ for 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.
      img
    Note:

    The above is a brief introduction to the way of publishing and subscribing to messages. For more operations, see Demo or Pulsar Python client.

    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