I am trying to call async functions in tornado overriden methods.
Initially, the function generate()
is syncronous, but that seems to block the entire program, including on_connection_close
method
class Reader:
def __init__(self, topic, group_id):
self.topic = topic
self.consumer = AIOKafkaConsumer(topic, bootstrap_servers="kafka:9092",
group_id=group_id)
async def read(self, sender):
print("enter read...")
#it seems the code below is blocking
await self.consumer.start()
async for message in self.consumer:
sender(json.dumps(consumed_message))
await self.consumer.commit()
class SampleSockeHandler(tornado.websocket.WebSocketHandler):
def on_connection_close(self):
self.app.close()
def on_message(self, message):
generate(message)
#await generate(message)
async def generate(self, message):
await self.reader.read(message)
print("enter read...")
is only executed once in the first read()
call. In the subsequent read()
calls, it does not print anymore.
Now when I comment out all the code below it i.e
await self.consumer.start()
async for message in self.consumer:
sender(json.dumps(consumed_message))
await self.consumer.commit()
it works again
How do I fix this problem? Shouldn't it be an async call?
async def main():
app = Application()
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
asyncio.run(main())