Problem Statement: I Was Using FastApi on Azure Function App and i was also using Beanie ODM for MongoDB and Beanie is an asynchronous ODM so it need to initialized at the startup event but as azure function are serveless so this cannot be done easily and i cannot also use asyncio.run() because FastAPI is handling it's own async loop to solve this issue it took me 2 days but finally fixed
Here's How I did It , I am not sure about the drawback's of this method but would love to hear if anyone knows that
connection_string = os.environ["DCS"]logging.info(f"connection string {connection_string}")async def init(): client = AsyncIOMotorClient(connection_string) await init_beanie(database=client['Tradex'],document_models=[Admin,User])loop = asyncio.get_running_loop()asyncio.set_event_loop(loop)loop.create_task(init())