def test_lock_multiple(self): # Lock + unlock a lock multiple times. fname = self.tempname() lock = platform.lock_file(fname) platform.unlock_file(lock) lock = platform.lock_file(fname) platform.unlock_file(lock)
def test_lock_multiple(self): fname = self.tempfile() lock = lock_file(fname) # The tests below would require flock() on Posix which does per-fd # locking instead of lockf() which is per process. However flock() is # not safe on NFS on all platforms. So disable these tests. #err = assert_raises(LockError, lock_file, fname) #assert hasattr(err, 'lock_pid') #assert err.lock_pid == os.getpid() #assert hasattr(err, 'lock_uid') #assert err.lock_uid == os.getuid() #assert hasattr(err, 'lock_cmd') #assert sys.argv[0].endswith(err.lock_cmd) unlock_file(lock) lock = lock_file(fname) unlock_file(lock)
def test_lock_locked_unix(self): # Lock a lock that is already locked. This should raise an OSError. fname = self.tempname() pid = os.fork() # The locks are per process. So need to fork here. if pid == 0: # child lock = platform.lock_file(fname) try: time.sleep(1) except KeyboardInterrupt: pass platform.unlock_file(lock) os._exit(0) time.sleep(0.1) self.assertRaises(OSError, platform.lock_file, fname) # Exit the child now, which will release the lock os.kill(pid, signal.SIGINT) os.waitpid(pid, 0) lock = platform.lock_file(fname) platform.unlock_file(lock)
def lock(self): """Lock the database.""" # I would have loved to use SQLite based locking but it seems # impossible to have a cross-connection and cross-transaction write # lock while still allowing reads from other connections (for e.g. # backups and getting lock information). lockname = '%s-lock' % self.filename try: self._lock = platform.lock_file(lockname) except platform.LockError as e: msg = 'Database is locked by process %d (%s)' % (e.lock_pid, e.lock_cmd) raise DatabaseError('Locked', msg) except Exception as e: raise DatabaseError('PlatformError', str(e))
def open(self, fname, lock=True): """Open the store file *fname*. If lock is *True* (the default), then the store file is locked using a platform specific file locking routine before it is opened. """ if self._connection is not None: raise RuntimeError('document store already opened') self._filename = fname if lock: self._lock = platform.lock_file(self._filename) self._connection = sqlite3.connect(fname, **self._sqlite_args) self._connection.create_function('get_json_value', 2, get_json_value) self._load_schema() self._depth = 0
def test_lock_unlock(self): fname = self.tempfile() lock = lock_file(fname) unlock_file(lock)
def test_lock_basic(self): # Ensure that a basic lock + unlock works. fname = self.tempname() lock = platform.lock_file(fname) platform.unlock_file(lock)