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.

  1. # copy header files
  2. cp -r /tubemq-cpp/include/tubemq /usr/local/include
  3. # copy third party and tubemq library files
  4. cp /tubemq-cpp/build/src/libtubemq.a \
  5. /tubemq-cpp/build/proto/libtubemq_proto.a \
  6. /tubemq-cpp/build/third_party/lib/lib* \
  7. /usr/local/lib64/

Install python3, requirements and python sdk

  1. # install requirements
  2. pip3 install -r requirements.txt
  3. # install python sdk
  4. python3 setup.py 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.

Producer

First of all, initialize tubemq.Producer

  1. import tubemq
  2. master_addr = "127.0.0.1:8000"
  3. producer = tubemq.Producer(master_addr)

then, publish the topic list

  1. topic_list = ["topic_0", "topic_1"]
  2. producer.publish(topic_list)

Construct the tubemq.Message and send

  1. send_data = "hello_tubemq"
  2. topic_name = "demo"
  3. msg = tubemq_message.Message(topic_name, send_data, len(send_data))
  4. if is_sync:
  5. result = producer.send(msg, is_sync=True)
  6. else:
  7. 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

  1. producer.stop()

Consumer

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

  1. import tubemq
  2. master_addr = "127.0.0.1:8000"
  3. topic_list = ["demo"]
  4. group_name = "test_group"
  5. consumer = tubemq.Consumer(master_addr, group_name, topic_list) # initialize consumer
  6. msgs = consumer.receive()
  7. if msgs:
  8. consumer.acknowledge()
  9. consumer.stop()

Example

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