miguelgrinberg / greenletio

Asyncio integration with sync code using greenlets.
MIT License
150 stars 8 forks source link

My first test and it's amazing #2

Closed Mdslino closed 4 years ago

Mdslino commented 4 years ago

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.

miguelgrinberg commented 4 years ago

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_().

Mdslino commented 4 years ago

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()
miguelgrinberg commented 4 years ago

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.

sylvoslee commented 4 years ago

So what is the correct application scenario for this project

miguelgrinberg commented 4 years ago

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: