def __init__(self, schema, db_path, close_idle=False): global next_db_id self.db_path = db_path self.db_dir = os.path.dirname(db_path) + "/" self.schema = schema self.schema["version"] = self.schema.get("version", 1) self.conn = None self.cur = None self.cursors = weakref.WeakSet() self.id = next_db_id next_db_id += 1 self.progress_sleeping = False self.commiting = False self.log = logging.getLogger("Db#%s:%s" % (self.id, schema["db_name"])) self.table_names = None self.collect_stats = False self.foreign_keys = False self.need_commit = False self.query_stats = {} self.db_keyvalues = {} self.delayed_queue = [] self.delayed_queue_thread = None self.close_idle = close_idle self.last_query_time = time.time() self.last_sleep_time = time.time() self.num_execute_since_sleep = 0 self.lock = ThreadPool.Lock() self.connect_lock = ThreadPool.Lock()
def testLockBlockingDifferentThread(self): lock = ThreadPool.Lock() def locker(): lock.acquire(True) time.sleep(0.5) lock.release() with ThreadPool.ThreadPool(10) as pool: threads = [ pool.spawn(locker), pool.spawn(locker), gevent.spawn(locker), pool.spawn(locker) ] time.sleep(0.1) s = time.time() lock.acquire(True, 5.0) unlock_taken = time.time() - s assert 1.8 < unlock_taken < 2.2 gevent.joinall(threads)
def testLockBlockingSameThread(self): lock = ThreadPool.Lock() s = time.time() def unlocker(): time.sleep(1) lock.release() gevent.spawn(unlocker) lock.acquire(True) lock.acquire(True, timeout=2) unlock_taken = time.time() - s assert 1.0 < unlock_taken < 1.5