KafkaConsumer prevents other asyncio.Task from running properly

Viewed 677

I am trying to create an async Python app that subscribes to a Kafka topic and prints out the received messages via the asyncio.Task named echo_kafka_msgs_task.

At the same time, there is a second asyncio.Task named ping_task that prints out an incrementing integer every second.

However, the integer is only printed out once, and no longer prints out when echo_kafka_msgs_task has started.

Why is this happening, and how can we fix this?

import asyncio
import json
import discord
from kafka import KafkaConsumer


class DiscordBot(discord.Client):
    def __init__(self):
        super().__init__()
        self.consumer = KafkaConsumer(
            "my_topic",
            value_deserializer=lambda m: json.loads(m.decode('utf-8'))
        )
        
    async def on_ready(self):
        ping_task = asyncio.create_task(self.ping())
        echo_kafka_msgs_task = asyncio.create_task(self.echo_kafka_msgs())
        
    async def echo_kafka_msgs(self):
        while True:
            self._logger.info("Waiting for new messages...")
            for msg in self.consumer:
                print(msg.value)
            
    async def ping(self):
        i = 0
        while True:
            print("i=", i)
            i += 1
            await asyncio.sleep(1)    

if __name__ == '__main__':
    client = DiscordBot()
    client.run("my_token")
0 Answers
Related