How to use rerun in a streamlit app that has nest asyncio and a lot of async (using asyncio) programming?

Hi,

I am running a streamlit app locally that needs to rerun when a certain if statement in a certain function is true. That function is the main logic holder function of the application. It is async and being run in the main block of (if name == “main”) using asyncio run.

The rerun statement is being triggered just fine. However, I always get the following error after the script is rerun and it encounters an await/async statement or function call during the rerun:-
RuntimeError: Task <Task pending name=‘Task-768’ coro=<BaseChatModel._agenerate_with_cache() running at C:\Users\heman\Downloads\ai_search_project\venv\Lib\site-packages\langchain_core\language_models\chat_models.py:924> cb=[gather.._done_callback() at C:\Python312\Lib\asyncio\tasks.py:767]> got Future <Task pending name=‘Task-769’ coro=<UnaryUnaryCall._invoke() running at C:\Users\heman\Downloads\ai_search_project\venv\Lib\site-packages\grpc\aio_call.py:568>> attached to a different loop

The streamlit application is using a lot of async programming using asyncio library. It also uses nest asyncio to patch nested loops (yes, I have tried removing nest asyncio but it didn’t work).

Please help!!!

UPDATE: Nevermind, I am dumb lol!

Hi - can you show a code snippet of the asyncio task? How are you creating a loop and running the async task with it?

Unfortunately, I can’t because the code is copyright protected. However, I fixed it and also realised that streamllit is not suited for my use case so I will be moving to chainlit instead.

Pity.