当前位置: 首页>>代码示例>>Python>>正文


Python AIOKafkaProducer.transaction方法代码示例

本文整理汇总了Python中aiokafka.producer.AIOKafkaProducer.transaction方法的典型用法代码示例。如果您正苦于以下问题:Python AIOKafkaProducer.transaction方法的具体用法?Python AIOKafkaProducer.transaction怎么用?Python AIOKafkaProducer.transaction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在aiokafka.producer.AIOKafkaProducer的用法示例。


在下文中一共展示了AIOKafkaProducer.transaction方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_producer_transactional_simple

# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import transaction [as 别名]
    async def test_producer_transactional_simple(self):
        # The test here will just check if we can do simple produce with
        # transactional_id option and minimal setup.

        producer = AIOKafkaProducer(
            loop=self.loop, bootstrap_servers=self.hosts,
            transactional_id="sobaka_producer")
        await producer.start()
        self.add_cleanup(producer.stop)

        async with producer.transaction():
            meta = await producer.send_and_wait(
                self.topic, b'hello, Kafka!')

        consumer = AIOKafkaConsumer(
            self.topic, loop=self.loop,
            bootstrap_servers=self.hosts,
            auto_offset_reset="earliest")
        await consumer.start()
        self.add_cleanup(consumer.stop)
        msg = await 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)
开发者ID:aio-libs,项目名称:aiokafka,代码行数:27,代码来源:test_transactional_producer.py

示例2: _test_control_record

# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import transaction [as 别名]
    async def _test_control_record(self, isolation_level):
        producer = AIOKafkaProducer(
            loop=self.loop, bootstrap_servers=self.hosts,
            transactional_id="sobaka_producer")
        await producer.start()
        self.add_cleanup(producer.stop)

        async with producer.transaction():
            meta = await producer.send_and_wait(
                self.topic, b'Hello from transaction', partition=0)

        consumer = AIOKafkaConsumer(
            self.topic, loop=self.loop,
            bootstrap_servers=self.hosts,
            auto_offset_reset="earliest",
            isolation_level=isolation_level,
            fetch_max_bytes=10)
        await consumer.start()
        self.add_cleanup(consumer.stop)

        # Transaction marker will be next after the message
        consumer.seek(meta.topic_partition, meta.offset + 1)

        with self.assertRaises(asyncio.TimeoutError):
            await asyncio.wait_for(
                consumer.getone(), timeout=0.5, loop=self.loop)

        # We must not be stuck on previous position
        position = await consumer.position(meta.topic_partition)
        self.assertEqual(position, meta.offset + 2)

        # After producing some more data it should resume consumption
        async with producer.transaction():
            meta2 = await producer.send_and_wait(
                self.topic, b'Hello from transaction 2', partition=0)

        msg = await consumer.getone()
        self.assertEqual(msg.offset, meta2.offset)
        self.assertEqual(msg.timestamp, meta2.timestamp)
        self.assertEqual(msg.value, b"Hello from transaction 2")
        self.assertEqual(msg.key, None)
开发者ID:aio-libs,项目名称:aiokafka,代码行数:43,代码来源:test_transactional_consumer.py

示例3: test_producer_transactional_fences_off_previous

# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import transaction [as 别名]
    async def test_producer_transactional_fences_off_previous(self):
        # Test 2 producers fencing one another by using the same
        # transactional_id

        producer = AIOKafkaProducer(
            loop=self.loop, bootstrap_servers=self.hosts,
            transactional_id="sobaka_producer", client_id="p1")
        await producer.start()
        self.add_cleanup(producer.stop)

        producer2 = AIOKafkaProducer(
            loop=self.loop, bootstrap_servers=self.hosts,
            transactional_id="sobaka_producer", client_id="p2")
        await producer2.start()
        self.add_cleanup(producer2.stop)
        async with producer2.transaction():
            await producer2.send_and_wait(self.topic, b'hello, Kafka! 2')

        with self.assertRaises(ProducerFenced):
            async with producer.transaction():
                await producer.send_and_wait(self.topic, b'hello, Kafka!')
开发者ID:aio-libs,项目名称:aiokafka,代码行数:23,代码来源:test_transactional_producer.py

示例4: test_producer_transactional_send_offsets_error_checks

# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import transaction [as 别名]
    async def test_producer_transactional_send_offsets_error_checks(self):
        producer = AIOKafkaProducer(
            loop=self.loop, bootstrap_servers=self.hosts,
            transactional_id="sobaka_producer", client_id="p1")
        await producer.start()
        self.add_cleanup(producer.stop)

        # Not in transaction
        with self.assertRaises(IllegalOperation):
            await producer.send_offsets_to_transaction({}, group_id=None)

        # Not proper group_id
        async with producer.transaction():
            with self.assertRaises(ValueError):
                await producer.send_offsets_to_transaction({}, group_id=None)
开发者ID:aio-libs,项目名称:aiokafka,代码行数:17,代码来源:test_transactional_producer.py

示例5: test_producer_require_transactional_id

# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import transaction [as 别名]
    async def test_producer_require_transactional_id(self):
        producer = AIOKafkaProducer(
            loop=self.loop, bootstrap_servers=self.hosts)
        await producer.start()
        self.add_cleanup(producer.stop)

        with self.assertRaises(IllegalOperation):
            await producer.begin_transaction()
        with self.assertRaises(IllegalOperation):
            await producer.commit_transaction()
        with self.assertRaises(IllegalOperation):
            await producer.abort_transaction()
        with self.assertRaises(IllegalOperation):
            async with producer.transaction():
                pass
        with self.assertRaises(IllegalOperation):
            await producer.send_offsets_to_transaction({}, group_id="123")
开发者ID:aio-libs,项目名称:aiokafka,代码行数:19,代码来源:test_transactional_producer.py

示例6: test_producer_transactional_aborting_previous_failure

# 需要导入模块: from aiokafka.producer import AIOKafkaProducer [as 别名]
# 或者: from aiokafka.producer.AIOKafkaProducer import transaction [as 别名]
    async def test_producer_transactional_aborting_previous_failure(self):
        # If we were to fail to send some message we should get\
        # OutOfOrderSequenceNumber

        producer = AIOKafkaProducer(
            loop=self.loop, bootstrap_servers=self.hosts,
            transactional_id="sobaka_producer", client_id="p1")
        await producer.start()
        self.add_cleanup(producer.stop)

        with self.assertRaises(OutOfOrderSequenceNumber):
            async with producer.transaction():
                await producer.send_and_wait(self.topic, b'msg1', partition=0)
                # Imitate a not delivered message
                producer._txn_manager.increment_sequence_number(
                    TopicPartition(self.topic, 0), 1)
                await producer.send_and_wait(self.topic, b'msg2', partition=0)
开发者ID:aio-libs,项目名称:aiokafka,代码行数:19,代码来源:test_transactional_producer.py


注:本文中的aiokafka.producer.AIOKafkaProducer.transaction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。