tencent cloud

Feedback

Use of Python SDK

Last updated: 2024-01-17 16:57:19

    Overview

    This document describes how to use an open-source SDK to send and receive messages with the SDK for Python serving as example, for you to better understand the complete procedure involved in message sending and receiving.

    Prerequisites

    Directions:

    Step 1: Preparing the Environment

    Rocketmq-client Python packaging is performed based on rocketmq-client-cpp. Therefore, librocketmq needs to be installed in advance.
    Note:
    Currently, the Python client only supports Linux and macOS operating systems, and Windows is not supported.
    1. Install librocketmq (version 2.0.0 or higher). For installation instructions, see librocketmq Installation.
    2. Execute the following command to install rocketmq-client-python.
    pip install rocketmq-client-python
    

    Step 2: Producing a Message

    Write codes for producing the message.
    from rocketmq.client import Producer, Message
    
    # Initialize the producer and set group information. group1
    producer = Producer(groupName)
    # Set the service address
    producer.set_name_server_address(nameserver)
    # Set permissions (role name and key)
    producer.set_session_credentials(
    accessKey, # Role key
    secretKey, # Role name
    ''
    )
    # Start the producer
    producer.start()
    
    # Assemble the message. The topic name can be copied from the Topic tab on the console.
    msg = Message(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()
    Parameter
    Description
    groupName
    Producer group name, which is obtained from the Group tab of cluster management on the console.
    nameserver
    Cluster access address in the basic information of the cluster. Select either the private network or public network access address as needed.
    
    secretKey
    Role name, which can be copied from SecretKey on the Cluster Permission page.
    accessKey
    Role key, which can be copied from AccessKey on the Cluster Permission page.
    
    topicName
    Topic name, which can be copied from the Topic tab on the console.
    TAGS
    Used to set the message tag.
    KEYS
    Used to configure the message business key.
    The Python client of the open-source community has certain defects in message production, resulting in an uneven load distribution among different queues of the same topic. For details, see Defect Details.

    Step 3: Consuming Messages

    Create, compile, and execute a consumption message program.
    import time
    
    from rocketmq.client import PushConsumer, ConsumeStatus
    
    
    # Callback for message processing.
    def callback(msg):
    # Simulate the business.
    print('Received message. messageId: ', msg.id, ' body: ', msg.body)
    # Return `CONSUME_SUCCESS` if the consumption is successful.
    return ConsumeStatus.CONSUME_SUCCESS
    # Return the status of the message upon successful consumption.
    # return ConsumeStatus.RECONSUME_LATER
    
    
    # Initialize the consumer and set the consumer group information.
    consumer = PushConsumer(groupName)
    # Set the service address
    consumer.set_name_server_address(nameserver)
    # Set permissions (role name and key)
    consumer.set_session_credentials(
    accessKey, # Role key
    secretKey, # Role name
    ''
    )
    # Subscribe to a topic.
    consumer.subscribe(topicName, callback, TAGS)
    print(' [Consumer] Waiting for messages.')
    # Start the consumer.
    consumer.start()
    
    while True:
    time.sleep(3600)
    # Release resources
    consumer.shutdown()
    Parameter
    Description
    groupName
    Consumer group name, which can be copied from the Group tab on the console.
    nameserver
    The same as the producer address.
    secretKey
    The same as the method of acquiring produced messages.
    accessKey
    The same as the method of acquiring produced messages.
    topicName
    Topic name, which can be copied from the Topic tab on the console.
    TAGS
    Set the tag of subscribed messages, which is set to * by default, indicating the subscription to all messages.

    Step 4: Viewing Consumption Details

    After the message is sent, you will receive a message ID (messageID). Developers can query the recently sent messages on the Message Query page, as shown in the following figure. Information such as details and traces for specific messages is also available. For details, see Message Query.
    
    Note:
    The preceding sections briefly describe how to publish and subscribe to messages. For more operations, see the Demo or the RocketMQ Client Python Examples.
    
    
    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