T
T
toddbarry2018-07-31 14:05:22
Python
toddbarry, 2018-07-31 14:05:22

Is it worth connecting orm to aiohttp through internal support?

aiohttp, judging by the documentation, supports Gino - this is an orm using the asyncpg driver, based on sqlalchemy.
Now this orm is connected by simple import to my backend:

from aiohttp import web
from gino import Gino

app = web.Application()
db = Gino()

class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    fullname = db.Column(db.String)

async def main():
    async with db.with_bind('postgresql://localhost/mydb') as engine:
        await db.gino.create_all()
        await User.create(name='jack', fullname='Jack Jones')
        print(await User.query.gino.all())

asyncio.get_event_loop().run_until_complete(main())

# дальше идут вьюхи, роуты и т п - пока всё в одном файле, т.к. мне так удобнее осваивать.
# Позднее всё будет отсортировано по различным модулям

if __name__ == '__main__':
    web.run_app(app)

Everything works
Here, as far as I can tell, two event loops are launched - one for aiohttp itself under the hood of the program, and the other for Gino.
However, the documentation describes support for Gino out of the box:
from aiohttp import web
from gino.ext.aiohttp import Gino

db = Gino()
app = web.Application(middlewares=[db])
db.init_app(app)

Then middlewares will work (by the way, I haven't figured out yet what exactly this middlewares db gives) The
question consists of two parts:
1) I haven't figured out yet whether it's correct to have two different event loops like in the first code? Do they somehow parallel each other? And if so, how? Won't one event loop block the second one for some reason?
2) If everything works well and so, what is the benefit of connecting Gino with the second option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2018-07-31
@vintello

and what native aiopg did not please?
there is also support for alchemy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question