-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
38 lines (30 loc) · 927 Bytes
/
db.py
File metadata and controls
38 lines (30 loc) · 927 Bytes
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
import asyncio
import aiosqlite
from contextlib import asynccontextmanager
async def get_connection(db_path: str):
conn = await aiosqlite.connect(db_path)
await conn.execute("PRAGMA journal_mode=WAL")
return conn
class WriterProvider:
def __init__(self, connection: aiosqlite.Connection):
self._connection = connection
self._lock = asyncio.Lock()
@asynccontextmanager
async def acquire(self):
await self._lock.acquire()
try:
yield self._connection
finally:
self._lock.release()
class ReaderProvider:
def __init__(self, connections):
self._pool = asyncio.Queue()
for conn in connections:
self._pool.put_nowait(conn)
@asynccontextmanager
async def acquire(self):
conn = await self._pool.get()
try:
yield conn
finally:
await self._pool.put(conn)