本文整理匯總了Python中aiokafka.producer.AIOKafkaProducer.commit_transaction方法的典型用法代碼示例。如果您正苦於以下問題:Python AIOKafkaProducer.commit_transaction方法的具體用法?Python AIOKafkaProducer.commit_transaction怎麽用?Python AIOKafkaProducer.commit_transaction使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類aiokafka.producer.AIOKafkaProducer
的用法示例。
在下文中一共展示了AIOKafkaProducer.commit_transaction方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_producer_transactional_send_batch_outside_txn
# 需要導入模塊: from aiokafka.producer import AIOKafkaProducer [as 別名]
# 或者: from aiokafka.producer.AIOKafkaProducer import commit_transaction [as 別名]
async def test_producer_transactional_send_batch_outside_txn(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)
batch = producer.create_batch()
batch.append(timestamp=None, key=None, value=b"2")
batch.close()
# Can not send if not yet in transaction
with self.assertRaises(IllegalOperation):
await producer.send_batch(batch, self.topic, partition=0)
await producer.begin_transaction()
await producer.send(self.topic, value=b"1", partition=0)
commit_task = ensure_future(producer.commit_transaction())
await asyncio.sleep(0.001, loop=self.loop)
self.assertFalse(commit_task.done())
# Already not in transaction
with self.assertRaises(IllegalOperation):
await producer.send_batch(batch, self.topic, partition=0)
await commit_task
# Transaction needs to be restarted
with self.assertRaises(IllegalOperation):
await producer.send_batch(batch, self.topic, partition=0)
示例2: test_producer_transactional_cancel_txn_methods
# 需要導入模塊: from aiokafka.producer import AIOKafkaProducer [as 別名]
# 或者: from aiokafka.producer.AIOKafkaProducer import commit_transaction [as 別名]
async def test_producer_transactional_cancel_txn_methods(self):
producer = AIOKafkaProducer(
loop=self.loop, bootstrap_servers=self.hosts,
transactional_id="sobaka_producer", client_id="p1")
txn_manager = producer._txn_manager
self.assertEqual(txn_manager.state, TransactionState.UNINITIALIZED)
await producer.start()
self.add_cleanup(producer.stop)
self.assertEqual(txn_manager.state, TransactionState.READY)
async def cancel(task):
# Coroutines will not be started until we yield at least 1ce
await asyncio.sleep(0)
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
# test cancel begin_transaction.
task = ensure_future(producer.begin_transaction())
await cancel(task)
self.assertEqual(txn_manager.state, TransactionState.READY)
# test cancel commit_transaction. Commit should not be cancelled.
await producer.begin_transaction()
self.assertEqual(txn_manager.state, TransactionState.IN_TRANSACTION)
task = ensure_future(producer.commit_transaction())
await cancel(task)
self.assertEqual(
txn_manager.state, TransactionState.COMMITTING_TRANSACTION)
await asyncio.sleep(0.1)
self.assertEqual(txn_manager.state, TransactionState.READY)
# test cancel abort_transaction. Abort should also not be cancelled.
await producer.begin_transaction()
self.assertEqual(txn_manager.state, TransactionState.IN_TRANSACTION)
task = ensure_future(producer.abort_transaction())
await cancel(task)
self.assertEqual(
txn_manager.state, TransactionState.ABORTING_TRANSACTION)
await asyncio.sleep(0.1)
self.assertEqual(txn_manager.state, TransactionState.READY)