Skip to content

Commit

Permalink
added test_multi_sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Shershen committed Oct 17, 2024
1 parent d95a2b0 commit cb29f3e
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pytest_cov.embed import multiprocessing_finishfrom tests.test_session import db_url

# SQLAlchemy FastAPI middleware

[![ci](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)
Expand Down Expand Up @@ -127,9 +129,10 @@ app.add_middleware(
routes.py

```python
import asyncio

from fastapi import APIRouter
from sqlalchemy import column
from sqlalchemy import table
from sqlalchemy import column, table, text

from databases import first_db, second_db

Expand All @@ -147,4 +150,22 @@ async def get_files_from_first_db():
async def get_files_from_second_db():
result = await second_db.session.execute(foo.select())
return result.fetchall()


@router.get("/concurrent-queries")
async def parallel_select():
async with first_db(multi_sessions=True):
async def execute_query(query):
return await first_db.session.execute(text(query))

tasks = [
asyncio.create_task(execute_query("SELECT 1")),
asyncio.create_task(execute_query("SELECT 2")),
asyncio.create_task(execute_query("SELECT 3")),
asyncio.create_task(execute_query("SELECT 4")),
asyncio.create_task(execute_query("SELECT 5")),
asyncio.create_task(execute_query("SELECT 6")),
]

await asyncio.gather(*tasks)
```

0 comments on commit cb29f3e

Please sign in to comment.