fastapi: Background task not executed on high load
I have a simple function that inserts in the database (RethinkDB) a generated user. The functions looks like this:
` @router.get(‘/api/v1/create_load_testing_user’) async def create_load_testing_user(request: Request, background_tasks: BackgroundTasks):
api_user_1 = copy.deepcopy(default_user_model)
api_user_1["uid"] = get_uuid_number()
api_user_1["profile"]["name"] = "API USER 1"
api_user_1["login"]["client_id"] = get_uuid_number()
api_user_1["login"]["client_secret"] = get_uuid_number()
api_user_1["is_performance_test_user"] = True
background_tasks.add_task(
login_test_performance_insert,
api_user_1,
request
)
return api_user_1
async def login_test_performance_insert(api_user_1, request):
await r.table("users").insert(api_user_1).run(await get_rethink_conn(request))
return True
`
Now, i am doing a performance test with AB:
ab -n 8000 -c 1000 https://link
And i am getting:
Complete requests: 8000 Failed requests: 6294 (Connect: 0, Receive: 0, Length: 6294, Exceptions: 0) Non-2xx responses: 1706
This means that i just lost 1706 user entries in the db
What am i doing wrong?
I have a http middleware that acquires a db connection, from a pool, before executing the route and releases it after the function is completed
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (6 by maintainers)
Because i want to return an answer faster. Without the background tasks i would get around 250 req/sec and with backgroundtasks i get 550 req/sec (with 8000 users in db ).