From 620cf34c2a2eba5b0321501de12cb63d14ef262f Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Mon, 9 Sep 2024 00:11:58 +0300 Subject: [PATCH] Move objects_lock into the Inventory object --- minode/shared.py | 1 - minode/sql.py | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/minode/shared.py b/minode/shared.py index 72864ec..a964dca 100644 --- a/minode/shared.py +++ b/minode/shared.py @@ -63,4 +63,3 @@ outgoing_connections = 8 connection_limit = 250 objects = {} -objects_lock = threading.Lock() diff --git a/minode/sql.py b/minode/sql.py index f1052c0..82cd4b5 100644 --- a/minode/sql.py +++ b/minode/sql.py @@ -3,6 +3,7 @@ import logging import os import sqlite3 +import threading import time from . import shared, structure @@ -13,6 +14,7 @@ sqlite3.threadsafety = 3 class Inventory(): """sqlite inventory""" def __init__(self): + self._lock = threading.Lock() self._db = sqlite3.connect( os.path.join(shared.data_directory, 'objects.dat'), check_same_thread=False @@ -53,7 +55,7 @@ class Inventory(): return cur def cleanup(self): - with shared.objects_lock: + with self._lock: self._db.execute( 'DELETE FROM objects WHERE expires < ?', (int(time.time()) - 3 * 3600,) @@ -126,14 +128,14 @@ class Inventory(): ) def __delitem__(self, vector): - with shared.objects_lock: # KeyError + with self._lock: # KeyError self._db.execute( 'DELETE FROM objects WHERE vector = ?', (vector,)) self._db.commit() self.rowid = len(self) def __setitem__(self, vector, obj): - with shared.objects_lock: + with self._lock: try: cur = self._db.execute( 'INSERT INTO objects VALUES (?,?,?,?,?,?,?,?)', (