()
| 64 | |
| 65 | |
| 66 | async def async_main(): |
| 67 | engine = create_async_engine( |
| 68 | "postgresql+asyncpg://scott:tiger@localhost/test", |
| 69 | echo=True, |
| 70 | ) |
| 71 | |
| 72 | async with engine.begin() as conn: |
| 73 | await conn.run_sync(Base.metadata.drop_all) |
| 74 | await conn.run_sync(Base.metadata.create_all) |
| 75 | |
| 76 | async_session = async_sessionmaker(engine, expire_on_commit=False) |
| 77 | |
| 78 | async with async_session() as session, session.begin(): |
| 79 | session.add_all([A(data="a_%d" % i) for i in range(100)]) |
| 80 | |
| 81 | statements = [ |
| 82 | select(A).where(A.data == "a_%d" % random.choice(range(100))) |
| 83 | for i in range(30) |
| 84 | ] |
| 85 | |
| 86 | frozen_results = await asyncio.gather( |
| 87 | *( |
| 88 | run_out_of_band(async_session, statement) |
| 89 | for statement in statements |
| 90 | ) |
| 91 | ) |
| 92 | results = [ |
| 93 | # merge_results means the ORM objects from the result |
| 94 | # will be merged back into the original session. |
| 95 | # load=False means we can use the objects directly without |
| 96 | # re-selecting them. however this merge operation is still |
| 97 | # more expensive CPU-wise than a regular ORM load because the |
| 98 | # objects are copied into new instances |
| 99 | ( |
| 100 | await session.run_sync( |
| 101 | merge_frozen_result, statement, result, load=False |
| 102 | ) |
| 103 | )() |
| 104 | for statement, result in zip(statements, frozen_results) |
| 105 | ] |
| 106 | |
| 107 | print(f"results: {[r.all() for r in results]}") |
| 108 | |
| 109 | |
| 110 | asyncio.run(async_main()) |
no test coverage detected