コード例 #1
0
    def subscribe(self, topic):
        def on_assign(consumer, partitions):
            log.info('subscribed')

        try:
            self.kafka_subscriber.subscribe([topic], on_assign=on_assign)
            
            while True:
                message = self.kafka_subscriber.poll(timeout=1.0)
                if self.killer.killed:
                    break
                
                if message is None:
                    continue

                if message.error():
                    log.error('read message error')
                
                # commit message
                self.kafka_subscriber.commit(asynchronous=False)

                log.info('received message from topic {t}'.format(t=message.topic()))
                print(message.value().decode('utf-8'))
        except:
            log.error('error subscribing to brokers')
        
        self.close()
コード例 #2
0
 def run(self):
     log.info('start kafka subscriber inside thread {tn}'.format(tn=self.name))
     self.subscribe(self.topic)
コード例 #3
0
 def on_assign(consumer, partitions):
     log.info('subscribed')
コード例 #4
0
 def on_delivery(err, msg):
     if err is not None:
         log.info('Failed to deliver message: %s: %s' %
                  (str(msg), str(err)))
     else:
         log.info('Message produced: %s' % (str(msg)))
コード例 #5
0
ファイル: killer.py プロジェクト: wuriyanto48/kafka-cli-py
 def kill(self, signum, frame):
     log.info('signum: {s} received'.format(s=signum))
     self.killed = True