0

I am trying to create a websocket server that receives input from user, and at the same time read from kafka and continuously send data to user. I am having trouble getting both to run concurrently.

async def sender(websocket):
    consumer = KafkaConsumer(...) # read from kafka
    for message in consumer:
        msg = json.loads(message.value.decode())   
        await websocket.send(msg)
 


async def handle_client(websocket):
    asyncio.create_task(sender(websocket))

    while True:
        print("Waiting for client input")
        try:
            msg = await websocket.recv()
            # do something with msg

async def main():
    async with websockets.serve(handle_client, "localhost", 8765):
        await asyncio.Future()  # run forever


if __name__ == "__main__":
    asyncio.run(main())

They seem to be blocking. I think the issuer is because sender() blocks it, it never sleeps?

jigiy43106
  • 21
  • 2

0 Answers0