при работе в связке falsk + sqlalchemy (async)?
RuntimeError: Task <Task pending name='Task-7' coro=<AsyncToSync.main_wrap() running at /Users/agolubov/Projects/github.com/andrei-golubov/basket-orders/venv/lib/python3.9/site-packages/asgiref/sync.py:353> cb=[_run_until_complete_cb() at /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py:184]> got Future <Future pending cb=[Protocol._on_waiter_completed()]> attached to a different loop
откуда у тебя другой луп берётся
Да вот решил поигратся, код примерно так выглядит: from flask import Flask, jsonify from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, selectinload from sqlalchemy.future import select from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine app = Flask(__name__) DATABASE_URL = "postgresql+asyncpg://username:password@localhost/mydatabase" engine = create_async_engine(DATABASE_URL, echo=True) Base = declarative_base() class A(Base): __tablename__ = "a" id = Column(Integer, primary_key=True) data = Column(String) bs = relationship("B") class B(Base): __tablename__ = "b" id = Column(Integer, primary_key=True) a_id = Column(Integer, ForeignKey("a.id")) data = Column(String) @app.route('/get_data') async def get_data(): async_session = AsyncSession(engine) async with async_session as session: stmt = select(A).options(selectinload(A.bs)) result = await session.execute(stmt) data = [] for a1 in result.scalars(): a_data = { 'id': a1.id, 'data': a1.data, 'bs': [{'id': b.id, 'data': b.data} for b in a1.bs] } data.append(a_data) return jsonify(data) if __name__ == "__main__": app.run()
Обсуждают сегодня