tencent cloud

Feedback

SDK for Python

Last updated: 2023-09-12 17:53:17

    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 the required resources as instructed in Resource Creation and Preparation.

    Directions

    Step 1. Prepare the environment

    As RocketMQ-client Python is lightweight wrapper around rocketmq-client-cpp, you need to install librocketmq first.
    Note
    Currently, the Python client only supports Linux and macOS operating systems. It doesn't support Windows systems.
    1. Install librocketmq 2.0.0 or later as instructed in Install librocketmq.
    2. Run the following command to install rocketmq-client-python.
    pip install rocketmq-client-python
    

    Step 2. Produce messages

    Create, compile, and run a message production program.
    from rocketmq.client import Producer, Message
    
    # Initialize the producer and set the producer group information. Be sure to use the full name of the group, such as `rocketmq-xxx|namespace_python%group1`.
    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. The topic name can be copied on the **Topic** page in 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 messages in sync mode
    ret = producer.send_sync(msg)
    print(ret.status, ret.msg_id, ret.offset)
    # Release resources
    producer.shutdown()
    Parameter
    Description
    groupName
    Producer group name, which can be obtained under the Group tab on the cluster details page in the console.
    nameserver
    Cluster access address, which can be copied from Access Address in the Operation column on the Cluster page in the console. Namespace access addresses in new virtual or exclusive clusters can be copied from the Namespace list.
    
    secretKey
    Role name, which can be copied on the Role Management page.
    accessKey
    Role token, which can be copied in the Token column on the Role Management page.
    
    topicName
    Topic name, which can be copied on the Topic page in the console.
    TAGS
    A parameter used to set the message tag.
    KEYS
    A parameter used to set the message key.
    There are certain defects in the message production of the open-source Python client, causing uneven load among different queues of the same Topic. For more information, see [RocketMQ document](https://github.com/apache/rocketmq-client-python/issues /128!cac28b204e4c02765f18ecd741ed1628).

    Step 3. Consume 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 processing logic
    print('Received message. messageId: ', msg.id, ' body: ', msg.body)
    # Return CONSUME_SUCCESS if the consumption is successful
    return ConsumeStatus.CONSUME_SUCCESS
    # Return the consumption status if the consumption is successful
    # 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 token)
    consumer.set_session_credentials(
    accessKey, # Role token
    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 under the Group tab on the cluster details page.
    nameserver
    Cluster access address, which can be copied from Access Address in the Operation column on the Cluster page in the console. Namespace access addresses in new virtual or exclusive clusters can be copied from the Namespace list.
    
    secretKey
    Role name, which can be copied on the Role Management page.
    accessKey
    Role token, which can be copied in the Token column on the Role Management page.
    
    topicName
    Topic name, which can be copied on the Topic page in the console.
    TAGS
    A parameter used to set the tag of messages that are subscribed to. The default value is set to *, which means subscribing to all messages.

    Step 4. View consumption details

    Log in to the TDMQ console, go to the Cluster > Group page, and view the list of clients connected to the group. Click View Details in the Operation column to view consumer details.
    
    
    Note
    Above is a brief introduction to message publishing and subscription. For more information, see Demo or RocketMQ-Client-Python Sample.
    
    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