Skip to content

Instantly share code, notes, and snippets.

@infnetdanpro
Created August 15, 2024 11:59
Show Gist options
  • Save infnetdanpro/f675be87827827cbeef5adc8b88d0605 to your computer and use it in GitHub Desktop.
Save infnetdanpro/f675be87827827cbeef5adc8b88d0605 to your computer and use it in GitHub Desktop.
FastAPI + ProcessPoolExecutor
import asyncio
from concurrent.futures import ProcessPoolExecutor
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING
from time import time
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: "FastAPI"):
max_workers = 5
app.state.executor = ProcessPoolExecutor(max_workers=max_workers)
print(f"Started ProcessPoolExecutor with max_workers={max_workers}")
yield
app.state.executor.shutdown()
async def run_in_process(executor, fn, *args):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(executor, fn, *args) # wait and return result
fastapi_app = FastAPI(docs_url="/api/docs", lifespan=lifespan)
...
@router.post("/avatar-upload/")
async def update_avatar(
request: Request,
img_base64: str = Body(..., description="Base64 image object"),
):
# some logic with base64 image
executor = request.app.state.executor
res = await run_in_process(executor, time)
return {'result': res}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment