Move objects_lock into the Inventory object
This commit is contained in:
parent
2e4152d347
commit
5d528e7e08
|
@ -63,4 +63,3 @@ outgoing_connections = 8
|
||||||
connection_limit = 250
|
connection_limit = 250
|
||||||
|
|
||||||
objects = {}
|
objects = {}
|
||||||
objects_lock = threading.Lock()
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from . import shared, structure
|
from . import shared, structure
|
||||||
|
@ -13,6 +14,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
|
||||||
|
@ -53,7 +55,7 @@ class Inventory():
|
||||||
return cur
|
return cur
|
||||||
|
|
||||||
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,)
|
||||||
|
@ -123,14 +125,14 @@ class Inventory():
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def __delitem__(self, vector):
|
def __delitem__(self, vector):
|
||||||
with shared.objects_lock: # KeyError
|
with self._lock: # KeyError
|
||||||
self._db.execute(
|
self._db.execute(
|
||||||
'DELETE FROM objects WHERE vector = ?', (vector,))
|
'DELETE FROM objects WHERE vector = ?', (vector,))
|
||||||
self._db.commit()
|
self._db.commit()
|
||||||
self.rowid = len(self)
|
self.rowid = len(self)
|
||||||
|
|
||||||
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 (?,?,?,?,?,?,?,?)', (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user