File size: 654 Bytes
cc6f785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import aiosqlite
import asyncio
import config

_db = None
_db_lock = asyncio.Lock()

async def get_db():
    global _db
    if _db is None:
        _db = await aiosqlite.connect(config.DB_PATH)
        await _db.execute("PRAGMA journal_mode=WAL")
        await _db.execute("PRAGMA busy_timeout=10000")
        await _db.commit()
    return _db

async def db_exec(func):
    async with _db_lock:
        return await func(await get_db())

class SharedDB:
    async def __aenter__(self):
        await _db_lock.acquire()
        return await get_db()
    async def __aexit__(self, *args):
        _db_lock.release()

def shared_db():
    return SharedDB()