I have three independet tables: OneTable, TwoTable, ThreeTable. There is 10k rows in each table. I try select all data from each table in parallel via async sqlalchemy scoped session. (https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html#using-asyncio-scoped-session).
That`t my session factory
engine_async = create_async_engine("postgresql+asyncpg://user:pass@host:port/db")
async_session_maker = async_sessionmaker(engine_async, expire_on_commit=False)
from sqlalchemy.ext.asyncio import (
async_scoped_session,
async_sessionmaker,
)
async_session_factory = async_sessionmaker(
async_session_maker,
expire_on_commit=False,
)
Function that create queries
async def some_function(num_table: int):
print(id(current_task()))
AsyncScopedSession = async_scoped_session(
async_session_maker,
scopefunc=current_task,
)
some_async_session = AsyncScopedSession()
start_time = time.monotonic()
if num_table == 1:
res = await some_async_session.execute(select(OneTable))
elif num_table == 2:
res = await some_async_session.execute(select(TwoTable))
elif num_table == 3:
res = await some_async_session.execute(select(ThreeTable))
print(f'time: ', time.monotonic() - start_time)
await some_async_session.commit()
await AsyncScopedSession.remove()
and even loop start:
async def start():
async with TaskGroup() as tg:
t1 = tg.create_task(some_function(1))
t2 = tg.create_task(some_function(2))
t3 = tg.create_task(some_function(3))
def main():
asyncio.run(start())
main()
When i start this program. I`ve seen log message about created tasks (id tasks). But time is:
time: 1.5
time: 3.0
time: 4.5
When I look at the pgAdmin, I see that three sessions are being created. At any given time, one or more of them are in the idle in transaction state. It looks like all sessions are waiting for one.
If I make queries sequentially, the result is almost the same. I know that one request takes 1.5 seconds.
What am I doing wrong? How to achieve parallel select requests.