-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstart_example.py
More file actions
44 lines (32 loc) · 1.08 KB
/
start_example.py
File metadata and controls
44 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Start example
import asyncio
from typing import Any, cast
from aiohttp import web
from psqlpy import PSQLPool
async def start_db_pool(app: web.Application) -> None:
"""Initialize database connection pool."""
db_pool = PSQLPool(
dsn="postgres://postgres:postgres@localhost:5432/postgres",
max_db_pool_size=2,
)
app["db_pool"] = db_pool
async def stop_db_pool(app: web.Application) -> None:
"""Close database connection pool."""
db_pool = cast("PSQLPool", app.db_pool)
await db_pool.close()
async def pg_pool_example(request: web.Request) -> Any:
db_pool = cast("PSQLPool", request.app["db_pool"])
connection = await db_pool.connection()
await asyncio.sleep(10)
query_result = await connection.execute(
"SELECT * FROM users",
)
dict_result = query_result.result()
return web.json_response(
data=dict_result,
)
application = web.Application()
application.on_startup.append(start_db_pool)
application.add_routes([web.get("/", pg_pool_example)])
if __name__ == "__main__":
web.run_app(application)