Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

use lock on db (#125)

Rory Clear58e72c958f9b
diff --git a/test/test_db.py b/test/test_db.py
index ec8455c..6fc4c17 100644
--- a/test/test_db.py
+++ b/test/test_db.py
@@ -1,8 +1,10 @@
from utils.db import db
import os
+import threading
-if __name__ == "__main__":
- #os.makedirs("data", exist_ok=True); open("data/cc_cache.db", "a").close()
+if __name__ == "__main__": # todo, use different file
+ os.remove("data/cc_cache.db") if os.path.exists("data/cc_cache.db") else None
+ os.makedirs("data", exist_ok=True); open("data/cc_cache.db", "a").close()
cache_db = db()
x = cache_db.run_get("links", None)
assert x == {}
@@ -35,4 +37,37 @@ if __name__ == "__main__":
x = cache_db.run_get("settings", None)
assert x == {'cam1': {'det': 'y'}}
x = cache_db.run_get("settings", "cam1", None)
- assert x == {'det': 'y'}
\ No newline at end of file
+ assert x == {'det': 'y'}
+
+ # test with threading
+ cache_db.run_put("max_storage", "all", 256)
+ for _ in range(100):
+ responses = []
+ def get_cross_thread(id):
+ try:
+ if id == 0:
+ resp = cache_db.run_get("links", None, timeout=2)
+ responses.append(("links", resp))
+ elif id == 1:
+ resp = cache_db.run_get("max_storage", "all", timeout=2)
+ responses.append(("max_storage", resp))
+ else:
+ resp = cache_db.run_get("settings", None, timeout=2)
+ responses.append(("settings", resp))
+ except Exception as e:
+ responses.append((f"thread{id}", f"ERROR: {e}"))
+ threads = []
+ responses.clear()
+ for i in range(3):
+ t = threading.Thread(target=get_cross_thread, args=(i,))
+ t.start()
+ threads.append(t)
+ for t in threads:
+ t.join()
+ wrong_type_count = 0
+ for table, resp in responses:
+ if table == "max_storage" and isinstance(resp, dict):
+ wrong_type_count += 1
+ elif table != "max_storage" and not isinstance(resp, dict):
+ wrong_type_count += 1
+ assert wrong_type_count == 0
\ No newline at end of file
diff --git a/utils/db.py b/utils/db.py
index e78ef48..e5d58ac 100644
--- a/utils/db.py
+++ b/utils/db.py
@@ -1,6 +1,7 @@
import os, sqlite3, contextlib, pickle
from typing import Any
from tinygrad.helpers import diskcache_get
+import threading
_db_tables = set()
cache_dir: str = "data/"
@@ -114,6 +115,7 @@ class db:
self._req_q = multiprocessing.Queue()
self._resp_q = multiprocessing.Queue()
self._stop = multiprocessing.Event()
+ self._lock = threading.Lock()
self._proc = multiprocessing.Process(
target=self._worker_loop,
@@ -159,19 +161,22 @@ class db:
self._proc.kill()
def run_get(self, table, key=None, id=None, timeout=5):
- self._req_q.put(("get", table, key, id))
- status, result = self._resp_q.get(timeout=timeout)
- if status == "err": raise Exception(result)
- return result
+ with self._lock:
+ self._req_q.put(("get", table, key, id))
+ status, result = self._resp_q.get(timeout=timeout)
+ if status == "err": raise Exception(result)
+ return result
def run_put(self, table, key, val=None, id=None, replace=True, timeout=5):
- self._req_q.put(("put", table, key, (val, id, replace)))
- status, result = self._resp_q.get(timeout=timeout)
- if status == "err": raise Exception(result)
- return result
+ with self._lock:
+ self._req_q.put(("put", table, key, (val, id, replace)))
+ status, result = self._resp_q.get(timeout=timeout)
+ if status == "err": raise Exception(result)
+ return result
def run_delete(self, table, key, id=None, timeout=5):
- self._req_q.put(("delete", table, key, id))
- status, result = self._resp_q.get(timeout=timeout)
- if status == "err": raise Exception(result)
- return result
\ No newline at end of file
+ with self._lock:
+ self._req_q.put(("delete", table, key, id))
+ status, result = self._resp_q.get(timeout=timeout)
+ if status == "err": raise Exception(result)
+ return result
\ No newline at end of file