Sunday, 21 February 2021

Sync & Async Python FastAPI scripts on Google App Engine

I've tried running the following py file script.py locally:

from fastapi import FastAPI
import asyncio
import time

app = FastAPI()

@app.get('/async/')
async def func(text: str):
    await asyncio.sleep(5)
    return {'text': text}


@app.get('/sync/')
async def func(text: str):
    time.sleep(5)
    return {'text': text}

using uvicorn script:app --reload

When making 10 concurrent requests to sync endpoint http://127.0.0.1:8000/sync/?text=test, each call takes 5 seconds and blocks the next, total time is 50 seconds

And when making 10 concurrent requests to async endpoint http://127.0.0.1:8000/async/?text=test, each call takes 5 seconds, total time is also 5 seconds as it's non-blocking.


I've tried deploying the script to Google App Engine using entrypoint: gunicorn -w 4 -k uvicorn.workers.UvicornWorker script:app and did the same tests instead of locally, and got the following results:

When making 10 concurrent requests to sync endpoint, the total time is 12 seconds, all of the 10 requests executed on the same 1 instance. ( 1000 concurrent requests took 60 seconds on 20+ instances )

And when making 10 concurrent requests to async endpoint, the total time is 5 seconds, all of the 10 requests executed on the same 1 instance. ( 1000 concurrent requests took 30 seconds on 20+ instances )


Why on GAE the sync code took 12 seconds for the 10 concurrent requests instead of 50 seconds?

And how can I run all 10 requests simultaneously on GAE for the sync endpoint, to get them all in 5 seconds?



from Sync & Async Python FastAPI scripts on Google App Engine

No comments:

Post a Comment