| import asyncio # Built-in, no pip install needed! | |
| from fastapi import FastAPI, UploadFile, File | |
| from cnnmodels import classify_tumor | |
| app = FastAPI(title="Brain Tumor CNN Service") | |
| async def classify(file: UploadFile = File(...)): | |
| # 1. This reads the file asynchronously on the Main Event Loop | |
| image_bytes = await file.read() | |
| # 2. Get the running manager (Event Loop) | |
| loop = asyncio.get_running_loop() | |
| # 3. Offload the heavy CNN model to the background thread pool | |
| # None tells Python to use FastAPI's default pool of worker threads | |
| result = await loop.run_in_executor(None, classify_tumor, image_bytes) | |
| return result |