Using both SQLALCHEMY session and scoped_session with the same engine in FastAPI

Viewed 552

I'm using sqlalchemy with a FastAPI server and I came across the following error when I called Session object from a multithread function (not via http requests):

QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30 (Background on this error at: http://sqlalche.me/e/13/3o7r)

This error only happens when my server is getting hundreds of requests and my thread workers are busy.

I read that sqlalchemy.scoped_session can be used for a background daemon.

Doing the following made the error disappear:

  1. Regular sqlalchemy Session object in the http requests, via FastAPI.Depends() just like it is recommend in the docs
  2. sqlalcehmy scoped_session - My server is running tasks in the background in different threads which need access to the database but each thread has its own session

Safe to say that each session object is being handled appropriately (scoped_session calls remove() and Session calls close())

My question is - is it not recommended using this approach when i'm using a single engine? Should I take a different path?

Thanks!

0 Answers
Related