本文整理汇总了Python中aiokafka.producer.AIOKafkaProducer.send_and_wait方法的典型用法代码示例。如果您正苦于以下问题:Python AIOKafkaProducer.send_and_wait方法的具体用法?Python AIOKafkaProducer.send_and_wait怎么用?Python AIOKafkaProducer.send_and_wait使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiokafka.producer.AIOKafkaProducer
的用法示例。
在下文中一共展示了AIOKafkaProducer.send_and_wait方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_producer_correct_time_returned
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_correct_time_returned(self):
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts)
yield from producer.start()
self.add_cleanup(producer.stop)
send_time = (time.time() * 1000)
res = yield from producer.send_and_wait(
"XXXX", b'text1', partition=0)
self.assertLess(res.timestamp - send_time, 1000) # 1s
res = yield from producer.send_and_wait(
"XXXX", b'text1', partition=0, timestamp_ms=123123123)
self.assertEqual(res.timestamp, 123123123)
expected_timestamp = 999999999
@asyncio.coroutine
def mocked_send(*args, **kw):
# There's no easy way to set LOG_APPEND_TIME on server, so use this
# hack for now.
return ProduceResponse[2](
topics=[
('XXXX', [(0, 0, 0, expected_timestamp)])],
throttle_time_ms=0)
with mock.patch.object(producer.client, 'send') as mocked:
mocked.side_effect = mocked_send
res = yield from producer.send_and_wait(
"XXXX", b'text1', partition=0)
self.assertEqual(res.timestamp_type, LOG_APPEND_TIME)
self.assertEqual(res.timestamp, expected_timestamp)
示例2: test_producer_send
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_send(self):
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts)
yield from producer.start()
self.add_cleanup(producer.stop)
with self.assertRaises(TypeError):
yield from producer.send(self.topic, 'hello, Kafka!', partition=0)
future = yield from producer.send(
self.topic, b'hello, Kafka!', partition=0)
resp = yield from future
self.assertEqual(resp.topic, self.topic)
self.assertTrue(resp.partition in (0, 1))
self.assertEqual(resp.offset, 0)
fut = yield from producer.send(self.topic, b'second msg', partition=1)
resp = yield from fut
self.assertEqual(resp.partition, 1)
future = yield from producer.send(self.topic, b'value', key=b'KEY')
resp = yield from future
self.assertTrue(resp.partition in (0, 1))
resp = yield from producer.send_and_wait(self.topic, b'value')
self.assertTrue(resp.partition in (0, 1))
yield from producer.stop()
with self.assertRaises(ProducerClosed):
yield from producer.send(self.topic, b'value', key=b'KEY')
示例3: test_producer_send
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_send(self):
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts)
yield from producer.start()
yield from self.wait_topic(producer.client, self.topic)
with self.assertRaisesRegexp(AssertionError, 'value must be bytes'):
yield from producer.send(self.topic, 'hello, Kafka!')
future = yield from producer.send(self.topic, b'hello, Kafka!')
resp = yield from future
self.assertEqual(resp.topic, self.topic)
self.assertTrue(resp.partition in (0, 1))
self.assertEqual(resp.offset, 0)
fut = yield from producer.send(self.topic, b'second msg', partition=1)
resp = yield from fut
self.assertEqual(resp.partition, 1)
future = yield from producer.send(self.topic, b'value', key=b'KEY')
resp = yield from future
self.assertTrue(resp.partition in (0, 1))
resp = yield from producer.send_and_wait(self.topic, b'value')
self.assertTrue(resp.partition in (0, 1))
yield from producer.stop()
with self.assertRaises(ProducerClosed):
yield from producer.send(self.topic, b'value', key=b'KEY')
示例4: test_producer_send_with_compression
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_send_with_compression(self):
with self.assertRaises(ValueError):
producer = AIOKafkaProducer(
loop=self.loop, compression_type='my_custom')
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts,
compression_type='gzip')
yield from producer.start()
yield from self.wait_topic(producer.client, self.topic)
# short message will not be compressed
future = yield from producer.send(
self.topic, b'this msg is too short for compress')
resp = yield from future
self.assertEqual(resp.topic, self.topic)
self.assertTrue(resp.partition in (0, 1))
# now message will be compressed
resp = yield from producer.send_and_wait(
self.topic, b'large_message-'*100)
self.assertEqual(resp.topic, self.topic)
self.assertTrue(resp.partition in (0, 1))
yield from producer.stop()
示例5: test_producer_send_batch
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_send_batch(self):
key = b'test key'
value = b'test value'
max_batch_size = 10000
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts,
max_batch_size=max_batch_size)
yield from producer.start()
partitions = yield from producer.partitions_for(self.topic)
partition = partitions.pop()
# silly method to find current offset for this partition
resp = yield from producer.send_and_wait(
self.topic, value=b'discovering offset', partition=partition)
offset = resp.offset
# only fills up to its limits, then returns None
batch = producer.create_batch()
self.assertEqual(batch.record_count(), 0)
num = 0
while True:
metadata = batch.append(key=key, value=value, timestamp=None)
if metadata is None:
break
num += 1
self.assertTrue(num > 0)
self.assertEqual(batch.record_count(), num)
# batch gets properly sent
future = yield from producer.send_batch(
batch, self.topic, partition=partition)
resp = yield from future
self.assertEqual(resp.topic, self.topic)
self.assertEqual(resp.partition, partition)
self.assertEqual(resp.offset, offset + 1)
# batch accepts a too-large message if it's the first
too_large = b'm' * (max_batch_size + 1)
batch = producer.create_batch()
metadata = batch.append(key=None, value=too_large, timestamp=None)
self.assertIsNotNone(metadata)
# batch rejects a too-large message if it's not the first
batch = producer.create_batch()
batch.append(key=None, value=b"short", timestamp=None)
metadata = batch.append(key=None, value=too_large, timestamp=None)
self.assertIsNone(metadata)
yield from producer.stop()
# batch can't be sent after closing time
with self.assertRaises(ProducerClosed):
yield from producer.send_batch(
batch, self.topic, partition=partition)
示例6: test_producer_sender_errors_propagate_to_producer
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_sender_errors_propagate_to_producer(self):
# Following on #362 there may be other unexpected errors in sender
# routine that we wan't the user to see, rather than just get stuck.
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts, linger_ms=1000)
yield from producer.start()
self.add_cleanup(producer.stop)
with mock.patch.object(producer._sender, '_send_produce_req') as m:
m.side_effect = KeyError
with self.assertRaisesRegex(
KafkaError, "Unexpected error during batch delivery"):
yield from producer.send_and_wait(
self.topic, b'hello, Kafka!')
with self.assertRaisesRegex(
KafkaError, "Unexpected error during batch delivery"):
yield from producer.send_and_wait(
self.topic, b'hello, Kafka!')
示例7: test_producer_notopic
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_notopic(self):
producer = AIOKafkaProducer(
loop=self.loop, request_timeout_ms=200,
bootstrap_servers=self.hosts)
yield from producer.start()
with mock.patch.object(
AIOKafkaClient, '_metadata_update') as mocked:
@asyncio.coroutine
def dummy(*d, **kw):
return
mocked.side_effect = dummy
with self.assertRaises(UnknownTopicOrPartitionError):
yield from producer.send_and_wait('some_topic', b'hello')
yield from producer.stop()
示例8: test_producer_invalid_leader_retry_metadata
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_invalid_leader_retry_metadata(self):
# See related issue #362. The metadata can have a new node in leader
# set while we still don't have metadata for that node.
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts, linger_ms=1000)
yield from producer.start()
self.add_cleanup(producer.stop)
# Make sure we have fresh metadata for partitions
yield from producer.partitions_for(self.topic)
# Alter metadata to convince the producer, that leader or partition 0
# is a different node
topic_meta = producer._metadata._partitions[self.topic]
topic_meta[0] = topic_meta[0]._replace(leader=topic_meta[0].leader + 1)
meta = yield from producer.send_and_wait(self.topic, b'hello, Kafka!')
self.assertTrue(meta)
示例9: test_producer_indempotence_no_duplicates
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_indempotence_no_duplicates(self):
# Indempotent producer should retry produce in case of timeout error
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts,
enable_idempotence=True,
request_timeout_ms=2000)
yield from producer.start()
self.add_cleanup(producer.stop)
original_send = producer.client.send
retry = [0]
@asyncio.coroutine
def mocked_send(*args, **kw):
result = yield from original_send(*args, **kw)
if result.API_KEY == ProduceResponse[0].API_KEY and retry[0] < 2:
retry[0] += 1
raise RequestTimedOutError
return result
with mock.patch.object(producer.client, 'send') as mocked:
mocked.side_effect = mocked_send
meta = yield from producer.send_and_wait(
self.topic, b'hello, Kafka!')
consumer = AIOKafkaConsumer(
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
auto_offset_reset="earliest")
yield from consumer.start()
self.add_cleanup(consumer.stop)
msg = yield from consumer.getone()
self.assertEqual(msg.offset, meta.offset)
self.assertEqual(msg.timestamp, meta.timestamp)
self.assertEqual(msg.value, b"hello, Kafka!")
self.assertEqual(msg.key, None)
with self.assertRaises(asyncio.TimeoutError):
yield from asyncio.wait_for(consumer.getone(), timeout=0.5)
示例10: test_producer_ssl
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_ssl(self):
# Produce by SSL consume by PLAINTEXT
topic = "test_ssl_produce"
context = self.create_ssl_context()
producer = AIOKafkaProducer(
loop=self.loop,
bootstrap_servers=[
"{}:{}".format(self.kafka_host, self.kafka_ssl_port)],
security_protocol="SSL", ssl_context=context)
yield from producer.start()
yield from producer.send_and_wait(topic=topic, value=b"Super msg")
yield from producer.stop()
consumer = AIOKafkaConsumer(
topic, loop=self.loop,
bootstrap_servers=self.hosts,
enable_auto_commit=True,
auto_offset_reset="earliest")
yield from consumer.start()
msg = yield from consumer.getone()
self.assertEqual(msg.value, b"Super msg")
yield from consumer.stop()
示例11: test_producer_indempotence_simple
# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import send_and_wait [as 别名]
def test_producer_indempotence_simple(self):
# The test here will just check if we can do simple produce with
# enable_idempotence option, as no specific API changes is expected.
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts,
enable_idempotence=True)
yield from producer.start()
self.add_cleanup(producer.stop)
meta = yield from producer.send_and_wait(self.topic, b'hello, Kafka!')
consumer = AIOKafkaConsumer(
self.topic, loop=self.loop,
bootstrap_servers=self.hosts,
auto_offset_reset="earliest")
yield from consumer.start()
self.add_cleanup(consumer.stop)
msg = yield from consumer.getone()
self.assertEqual(msg.offset, meta.offset)
self.assertEqual(msg.timestamp, meta.timestamp)
self.assertEqual(msg.value, b"hello, Kafka!")
self.assertEqual(msg.key, None)