首页 > 编程语言 > 详细

Kafka Consumer(Python threading)

时间:2017-07-31 00:22:47      阅读:444      评论:0      收藏:0      [点我收藏+]
import threading
from kafka import KafkaConsumer

threads = []

class MyThread(threading.Thread):
def __init__(self, threadName, keyName):
threading.Thread.__init__(self)
self.threadName=threadName
self.keyName=keyName

def run(self):
receiveinfo(self.threadName, self.keyName)

def receiveinfo(threadName, keyName):
consumer = KafkaConsumer(‘test‘, bootstrap_servers=‘192.168.1.10:9092‘)
for msg in consumer:
if msg.key==keyName:
if msg.value=="exit": break
print("("+threadName+")"+ " " + msg.value)
try:
t1=MyThread("T1","Thread-1")
threads.append(t1)
t2=MyThread("T2","Thread-2")
threads.append(t2)
t3=MyThread("T3","Thread-3")
threads.append(t3)

for t in threads:
t.start()

for t in threads:
t.join()

print ("exit program with 0")
except:
print ("Error: failed to run consumer program")

Kafka Consumer(Python threading)

原文:http://www.cnblogs.com/michaelying/p/7260890.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!