I am using the generate_content_async function to generate text from images asynchronously. The function works perfectly in Google Colab but does not behave as expected in a Flask app. On second call getting error as "Event loop is closed"

Code Implementation

Image Description Function

`type or paste code here`
async def describe_local_images():
    for img_filename in img_filenames:
        img = PIL.Image.open(img_filename)
        r = await model.generate_content_async([prompt, img])
        print(r.text)
await describe_local_images()

This works as expected in Colab but fails in the Flask environment.

Workaround in Flask

To handle the event loop issue, I implemented the following workaround:

python CopyEdit

def run_async_process(coroutine, *args, **kwargs):
    """Safely runs the given coroutine in a new event loop."""
    try:
        # Create a new event loop
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        # Run the coroutine
        asyncio.run(process_image(*args, **kwargs))
    finally:
        # Ensure the loop is closed properly
        loop.close()
async def process_image(imgs, model, prompt, document_id):
    jobs = asyncio.gather(
        *[process_image_util(img, model, prompt) for img in imgs]
    )
    results = await jobs
    mongo.db.extracted_text.insert_one({
        "document_id": document_id,
        "text_data": json.dumps(results),
        "date": datetime.datetime.utcnow()
    })
async def process_image_util(img: str, model, prompt_main) -> str:
    doc_name = img.split(os.sep)[-2]
    page_no = img.split(os.sep)[-1]
    try:
        r = await model.generate_content_async([prompt_main, PIL.Image.open(img)])
        return {"doc_name": doc_name, "doc_text": r.text, "page_no": page_no}
    except Exception as e:
        print("Error processing image:", e)
        return {"doc_name": doc_name, "doc_text": "error", "page_no": page_no}

Even with this workaround, the generate_content_async function seems to rely on the first event loop, causing failures when the loop is closed or reused.

Error : Event loop is closed

  1. Why does generate_content_async behave differently in Colab versus a Flask app?

Environment Details
• Module: google.generativeai
• Function: generate_content_async
• Framework: Flask
• Python Version: 3.10.14
• Additional Libraries: PIL, asyncio

reference : cookbook/quickstarts/Asynchronous_requests.ipynb at main · google-gemini/cookbook · GitHub