Skip to main content
Version: 1.5.0

Python SDK

Build TubeMQ Python SDK

Python SDK is a wrapper of C++ SDK through pybind11, so before building Python SDK,ensure the C++ SDK has built. The build process of C++ SDK is in Build TubeMQ C++ SDK

Then install the C++ SDK library and header files.

# copy header files
cp -r /tubemq-cpp/include/tubemq /usr/local/include

# copy third party and tubemq library files
cp /tubemq-cpp/build/src/libtubemq.a \
/tubemq-cpp/build/proto/libtubemq_proto.a \
/tubemq-cpp/build/third_party/lib/lib* \

Install python3, requirements and python sdk

# install requirements
pip3 install -r requirements.txt

# install python sdk
python3 install

After installation,there are compliled so files and python package in build/lib, they can be added to python's site-packages or PYTHONPATH

Python SDK API

Similar with C++ SDK ,Python SDK is also divided into Producer and Consumer.


First of all, initialize tubemq.Producer

import tubemq

master_addr = ""
producer = tubemq.Producer(master_addr)

then, publish the topic list

topic_list = ["topic_0", "topic_1"]

Construct the tubemq.Message and send

send_data = "hello_tubemq"
topic_name = "demo"
msg = tubemq_message.Message(topic_name, send_data, len(send_data))
if is_sync:
result = producer.send(msg, is_sync=True)
producer.send(msg, callback=func) # default is async send

The Python SDK also supports synchronous and asynchronous sending methods, which are similar to C++ SDK. When sending asynchronously, a callable object should be provided.

Finally, stop the producer



Consumer API of Python SDK is similar with Producer, this is a simple example

import tubemq

master_addr = ""
topic_list = ["demo"]
group_name = "test_group"
consumer = tubemq.Consumer(master_addr, group_name, topic_list) # initialize consumer

msgs = consumer.receive()
if msgs:



For more detailed python sdk use cases, please refer to Python SDK Example