#!/usr/local/bin/python import time from datetime import datetime, timedelta terminate = datetime.now() + timedelta(seconds=20) from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers='kafka:9092') count = 1 while True: #datetime.now() < terminate: text = "Hello {c}".format(c=count) print(text) producer.send("sample", bytes(text, encoding="utf-8")) # producer.send("sample", b"foobar") time.sleep(0.5) count += 1