From 91dbd5eea8d3f3a289dcee428f9db6e8dc9eee19 Mon Sep 17 00:00:00 2001 From: Siddharth Ravikumar Date: Thu, 19 Mar 2015 15:26:33 -0400 Subject: combox/silo.py: All access to DB are now inside a critcal section. This just means, only one damn Thread is allowed to access the DB at a time. We are using thread.Lock to achieve this. --- combox/silo.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/combox/silo.py b/combox/silo.py index e08eafc..a2a6b9b 100644 --- a/combox/silo.py +++ b/combox/silo.py @@ -19,6 +19,7 @@ import pickledb from os import path +from threading import Lock from combox.file import hash_file @@ -38,6 +39,8 @@ class ComboxSilo(object): silo = path.join(config['silo_dir'], 'silo.db') self.db = pickledb.load(silo, True) + self.lock = Lock() + def update(self, filep): """Update filep's info in db @@ -47,17 +50,17 @@ class ComboxSilo(object): filep: path to the file in combox directory. """ - - fhash = hash_file(filep) - - return self.db.set(filep, fhash) + with self.lock: + fhash = hash_file(filep) + return self.db.set(filep, fhash) def keys(self): """Returns a list of all keys in db.""" # this is why Redis or some other key-value DB should be used # instead of PickleDB - return self.db.db.keys() + with self.lock: + return self.db.db.keys() def remove(self, filep): @@ -67,7 +70,8 @@ class ComboxSilo(object): """ try: - return self.db.rem(filep) + with self.lock: + return self.db.rem(filep) except KeyError, e: # means `filep' not present in db. return False @@ -82,10 +86,11 @@ class ComboxSilo(object): """ - if self.db.get(filep) is None: - return False - else: - return True + with self.lock: + if self.db.get(filep) is None: + return False + else: + return True def stale(self, filep, fhash=None): @@ -101,7 +106,8 @@ class ComboxSilo(object): if not fhash: fhash = hash_file(filep) - fhash_in_db = self.db.get(filep) + with self.lock: + fhash_in_db = self.db.get(filep) if fhash_in_db is None: return None -- cgit v1.2.3