confluent_kafka生产者

import socket

from confluent_kafka import Producer

conf = {
    'bootstrap.servers': "localhost:9092",
    'client.id': socket.gethostname()
}

producer = Producer(conf)


def __publish_delivery_report(err, msg) -> None:
    if err is not None:
        print(f"send msg:{msg} fail, err is not None")
    else:
        print(f"send msg{msg} success")


def send_msg(topic: str, data):
    producer.produce(topic, data, callback=__publish_delivery_report)
    producer.flush()


if __name__ == '__main__':
    msg = "hello kafka"
    topic = "test"
    send_msg(topic, msg)

 

上一篇:java关键字,保留字


下一篇:Blog.042 Shell 编程 免交互