My first test and it's amazing
Closed this issue · 5 comments
I did a little test with a application i have written in FastAPI using SQLAlchemy as ORM and the results are amazing:
The route code is the following:
@router.post('/sync_user')
async def sync_user(login: schemas.Login, db: Session = Depends(get_db)):
start = datetime.now()
if user := crud.login.get_by_email(db, login.email):
end = datetime.now()
duration = end - start
logger.info(f'Login Sync Duration: {duration.total_seconds()}')
return user
@router.post('/async_user')
async def async_user(login: schemas.Login, db: Session = Depends(get_db)):
start = datetime.now()
if user := await async_(crud.login.get_by_email)(db, login.email):
end = datetime.now()
duration = end - start
logger.info(f'Login Async Duration: {duration.total_seconds()}')
return user
Login Sync Duration: 0.015734
Login Async Duration: 0.002904
The results are outstanding
Really thanks for this. I'll test more later this week.
What does crud.login.get_by_email()
do? The point of this library is not to magically convert sync code to async. If this code is blocking, it will still be blocking after you wrap with with async_()
.
It's just a database query with SQLAlchemy Session on it.
def get_by_email(self, db: Session, email: str) -> Optional[User]:
return db.query(self.model).filter_by(email=email).first()
Right, so this does not give you any benefit, you are still blocking. Not sure why your timings say otherwise, but this project isn't designed to help in your case. You shouldn't be using SQLAlchemy in an asynchronous application.
So what is the correct application scenario for this project
The goal is to allow regular and async functions to call each other. There is nothing that makes blocking code non-blocking, at least nothing at this time.
Use cases:
- use an async library from regular code
- use an async handler in a non-async web framework such as Flask