本文整理汇总了Python中kafka.producer.SimpleProducer.stop方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleProducer.stop方法的具体用法?Python SimpleProducer.stop怎么用?Python SimpleProducer.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kafka.producer.SimpleProducer
的用法示例。
在下文中一共展示了SimpleProducer.stop方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: KafkaProducer
# 需要导入模块: from kafka.producer import SimpleProducer [as 别名]
# 或者: from kafka.producer.SimpleProducer import stop [as 别名]
class KafkaProducer(KafkaBase, TerminatorBlock):
""" A block for producing Kafka messages """
version = VersionProperty("1.0.1")
def __init__(self):
super().__init__()
self._producer = None
def configure(self, context):
super().configure(context)
self._connect()
def stop(self):
self._disconnect()
super().stop()
def process_signals(self, signals, input_id='default'):
msgs = []
for signal in signals:
if self.connected:
try:
if type(signal) is not bytes:
signal = pickle.dumps(signal)
except:
self.logger.exception(
"Signal: {0} could not be serialized".format(signal))
return
msgs.append(signal)
else:
return
try:
if self.connected:
self._producer.send_messages(self._encoded_topic, *msgs)
except:
self.logger.exception("Failure sending signal")
def _connect(self):
super()._connect()
self._producer = SimpleProducer(self._kafka)
def _disconnect(self):
if self._producer:
self._producer.stop()
self._producer = None
super()._disconnect()
@property
def connected(self):
return super().connected and self._producer
示例2: __init__
# 需要导入模块: from kafka.producer import SimpleProducer [as 别名]
# 或者: from kafka.producer.SimpleProducer import stop [as 别名]
class KafkaProducer:
def __init__(self,conn_pool,topic):
self.conn_pool = conn_pool
self.topic = topic
self.kafka = KafkaClient(self.conn_pool)
self.producer = SimpleProducer(self.kafka, async=True)
def send(self,message):
self.producer.send_messages(self.topic, message)
def sendBulk(self,messages):
self.producer.send_messages(self.topic, *messages)
def close(self):
self.producer.stop()
self.kafka.close()
self.kafka = None
self.producer = None
示例3: __init__
# 需要导入模块: from kafka.producer import SimpleProducer [as 别名]
# 或者: from kafka.producer.SimpleProducer import stop [as 别名]
class Producer:
'Simple kafka producer'
def __init__(self, servers, topic):
self.client = KafkaClient(servers)
self.producer = SimpleProducer(self.client)
self.topic = topic
def current_time(self):
return int(round(time.time() * 1000))
def produce(self, mesnum):
msg = json.dumps({'time': self.current_time(), 'data' : 'Hello - %s' % mesnum})
self.producer.send_messages(self.topic, msg)
def stop(self):
if self.producer != None:
self.producer.stop()
if self.client != None:
self.client.close()
示例4: int
# 需要导入模块: from kafka.producer import SimpleProducer [as 别名]
# 或者: from kafka.producer.SimpleProducer import stop [as 别名]
import threading, sys, json, time
from kafka.client import KafkaClient
from kafka.producer import SimpleProducer
from datetime import datetime
if __name__ == "__main__":
servers = sys.argv[1]
topic = sys.argv[2]
mesnum = int(sys.argv[3])
interval = float(sys.argv[4])
client = KafkaClient(servers)
producer = SimpleProducer(client)
current_time = lambda: int(round(time.time() * 1000))
try:
while mesnum > 0:
msg = json.dumps({'time': current_time(), 'data' : 'Hello - %s' % mesnum})
producer.send_messages(topic, msg)
time.sleep(interval)
mesnum -= 1
except KeyboardInterrupt:
print "Interrupted!"
finally:
producer.stop()
client.close()