2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Remove threading.Lock usages

This commit is contained in:
Dan 2022-12-23 20:20:27 +01:00
parent ccb58f503c
commit 81573bce76

View File

@ -19,7 +19,6 @@
import inspect import inspect
import sqlite3 import sqlite3
import time import time
from threading import Lock
from typing import List, Tuple, Any from typing import List, Tuple, Any
from pyrogram import raw from pyrogram import raw
@ -98,10 +97,9 @@ class SQLiteStorage(Storage):
super().__init__(name) super().__init__(name)
self.conn = None # type: sqlite3.Connection self.conn = None # type: sqlite3.Connection
self.lock = Lock()
def create(self): def create(self):
with self.lock, self.conn: with self.conn:
self.conn.executescript(SCHEMA) self.conn.executescript(SCHEMA)
self.conn.execute( self.conn.execute(
@ -119,24 +117,20 @@ class SQLiteStorage(Storage):
async def save(self): async def save(self):
await self.date(int(time.time())) await self.date(int(time.time()))
self.conn.commit()
with self.lock:
self.conn.commit()
async def close(self): async def close(self):
with self.lock: self.conn.close()
self.conn.close()
async def delete(self): async def delete(self):
raise NotImplementedError raise NotImplementedError
async def update_peers(self, peers: List[Tuple[int, int, str, str, str]]): async def update_peers(self, peers: List[Tuple[int, int, str, str, str]]):
with self.lock: self.conn.executemany(
self.conn.executemany( "REPLACE INTO peers (id, access_hash, type, username, phone_number)"
"REPLACE INTO peers (id, access_hash, type, username, phone_number)" "VALUES (?, ?, ?, ?, ?)",
"VALUES (?, ?, ?, ?, ?)", peers
peers )
)
async def get_peer_by_id(self, peer_id: int): async def get_peer_by_id(self, peer_id: int):
r = self.conn.execute( r = self.conn.execute(
@ -185,7 +179,7 @@ class SQLiteStorage(Storage):
def _set(self, value: Any): def _set(self, value: Any):
attr = inspect.stack()[2].function attr = inspect.stack()[2].function
with self.lock, self.conn: with self.conn:
self.conn.execute( self.conn.execute(
f"UPDATE sessions SET {attr} = ?", f"UPDATE sessions SET {attr} = ?",
(value,) (value,)
@ -221,7 +215,7 @@ class SQLiteStorage(Storage):
"SELECT number FROM version" "SELECT number FROM version"
).fetchone()[0] ).fetchone()[0]
else: else:
with self.lock, self.conn: with self.conn:
self.conn.execute( self.conn.execute(
"UPDATE version SET number = ?", "UPDATE version SET number = ?",
(value,) (value,)