Move objects_lock into the Inventory object

This commit is contained in:
Lee Miller 2024-09-09 00:11:58 +03:00
parent 2e5fd98994
commit 0b10a7f4f4
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
2 changed files with 5 additions and 4 deletions

View File

@ -63,4 +63,3 @@ outgoing_connections = 8
connection_limit = 250 connection_limit = 250
objects = {} objects = {}
objects_lock = threading.Lock()

View File

@ -2,6 +2,7 @@
import os import os
import sqlite3 import sqlite3
import threading
import time import time
from . import shared, structure from . import shared, structure
@ -12,6 +13,7 @@ sqlite3.threadsafety = 3
class Inventory(): class Inventory():
"""sqlite inventory""" """sqlite inventory"""
def __init__(self): def __init__(self):
self._lock = threading.Lock()
self._db = sqlite3.connect( self._db = sqlite3.connect(
os.path.join(shared.data_directory, 'objects.dat'), os.path.join(shared.data_directory, 'objects.dat'),
check_same_thread=False check_same_thread=False
@ -37,7 +39,7 @@ class Inventory():
) )
def cleanup(self): def cleanup(self):
with shared.objects_lock: with self._lock:
self._db.execute( self._db.execute(
'DELETE FROM objects WHERE expires < ?', 'DELETE FROM objects WHERE expires < ?',
(int(time.time()) - 3 * 3600,) (int(time.time()) - 3 * 3600,)
@ -110,7 +112,7 @@ class Inventory():
) )
def __delitem__(self, vector): def __delitem__(self, vector):
with shared.objects_lock: # KeyError with self._lock: # KeyError
cur = self._db.execute( cur = self._db.execute(
'DELETE FROM objects WHERE vector = ?', (vector,)) 'DELETE FROM objects WHERE vector = ?', (vector,))
self._db.commit() self._db.commit()
@ -118,7 +120,7 @@ class Inventory():
self.rowid = len(self) or None self.rowid = len(self) or None
def __setitem__(self, vector, obj): def __setitem__(self, vector, obj):
with shared.objects_lock: with self._lock:
try: try:
cur = self._db.execute( cur = self._db.execute(
'INSERT INTO objects VALUES (?,?,?,?,?,?,?,?)', ( 'INSERT INTO objects VALUES (?,?,?,?,?,?,?,?)', (