Exemplo n.º 1
0
async def send_one():
    producer = AIOKafkaProducer(loop=loop, bootstrap_servers='localhost:9092')
    # Get cluster layout and initial topic/partition leadership information
    await producer.start()
    try:
        calls = []
        for i in range(0, count):
            calls.append(
                producer.send_and_wait("mypart", str.encode(f"Message {i}")))
        await asyncio.gather(*calls)
    finally:
        # Wait for all pending messages to be delivered or expire.
        await producer.stop()
Exemplo n.º 2
0
async def send(loop, topics, total_msg=100):

    producer = AIOKafkaProducer(
        loop=loop,
        bootstrap_servers=[
            'srv-kafka1:9092',
            'srv-kafka2:9092'
        ]
    )
    await producer.start()
    try:
        await asyncio.wait([
            producer.send_and_wait(topic, f'Super message: {count}'.encode())
            for topic in topics for count in range(total_msg)
        ])
    finally:
        # Wait for all pending messages to be delivered or expire.
        await producer.stop()