full-stack-fastapi-template
full-stack-fastapi-template copied to clipboard
SQLAlchemy 1.4 and asyncio
As mentioned in the coming SQLAlchemy 1.4 release note, there will be asyncio based connection and session.
Are there plans to switch to async endpoints ?
Sqlalchemy 1.4 was released - https://www.sqlalchemy.org/blog/2021/03/15/sqlalchemy-1.4.0-released/
bump on this. @tiangolo is there something we can contribute to get this feature?
thanks
I managed to upgrade to SQLAlchemy 1.4 and convert endpoints to asyncio in https://github.com/tiangolo/full-stack-fastapi-postgresql/pull/359. Unfortunately, I couldn't get Celery to work nicely with asyncio yet.
https://github.com/tiangolo/full-stack-fastapi-postgresql/pull/359
hi guys, I want to contribute my gist here - https://gist.github.com/sandys/671b8b86ba913e6436d4cb22d04b135f
This is fastapi with python dataclasses - used to create both sqlalchemy and pydantic models simultaneously (there is no double definition of models). I have added a bunch of examples including indexes, mixins, etc etc In addition - i have both sync and asyncpg examples (using the new sqlalchemy async support). Would love comments. When I benchmarked using locust, I get 400 rps using the gunicorn commandline in the gist.
On Sat, Apr 3, 2021 at 4:18 PM Jian Yuan Lee @.***> wrote:
I managed to upgrade to SQLAlchemy 1.4 and convert endpoints to asyncio in #359 https://github.com/tiangolo/full-stack-fastapi-postgresql/pull/359. Unfortunately, I couldn't get Celery to work nicely with asyncio yet.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/315#issuecomment-812848236, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAASYUYASQXKM4KGB6CSY7DTG3XAHANCNFSM4TVPV3JQ .
Hi, any news about this thread? I'm struggling with async engine and still there are problems that I can't figure them out. I'm using asynpg btw :D
@jianyuan , I had the same problem. I'm using the following solution:
def coroutine(function: Callable):
@wraps(function)
def wrapper(*args, **kwargs):
asyncio.run(function(*args, **kwargs))
return wrapper
@celery.task
@coroutine
async def execute_task():
# the task code here
I'm using the same decorator for typer too:
@cli.command()
@coroutine
async def some_func(
opt1: str = Option(...),
opt2: str = Option(...)
):
# your code here
It adds additional overhead of course, but it's not critical for my setup. It's nice I can use AsyncEngine and AsyncSession everywhere in the code.
Thanks all! The project now uses SQLModel, which is based on SQLAlchemy 2.0. :tada:
The idea is to keep the project simple and easy to extend and update, so, by default, functions are regular, not async, but you can use async in your own code bases for the cases where it makes sense, making sure all the internal code is compatible.
SQLModel, as is based on SQLAlchemy 2.0, already supports async too (although it's not properly documented yet). But you can already use it. :nerd_face: