def lock_getter1(lock_dir, lock_id, qs2m, qm2s): l = Lock(lock_dir, lock_id) got_lock = l.acquire_if_available() qs2m.put(got_lock) l.acquire() qs2m.put(True) d = qm2s.get() l.release()
def lock_cleanup_slave(lock_dir, lock_id, pidfile): os.closerange(0, resource.RLIMIT_NOFILE) pid = os.fork() if pid == 0: l = Lock(lock_dir, lock_id) l.acquire() with open(pidfile, "w") as f: f.write(str(os.getpid()) + "\n") while True: time.sleep(1) else: sys.exit(0)
def test_lock_cleanup(self): l = Lock(self.dir, "id1") pidfile = os.path.join(self.dir, "cleanup.pid") subprocess.check_call([sys.executable, __file__, "--slave", self.dir, "id1", pidfile], shell=False) while True: logger.debug("Checking for pidfile %s" % pidfile) if os.path.exists(pidfile): with open(pidfile, "r") as f: d = f.read() if d.endswith("\n"): pid = int(d.rstrip()) break time.sleep(1) os.kill(pid, 9) logger.debug("Attempting to acquire lock %s requiring cleanup of process %d" % (l.path, pid)) l.acquire() # can only get after slave process exits
def test_multiproc1(self): l = Lock(self.dir, "id1") l.acquire() qs2m = Queue() qm2s = Queue() p = Process(target=lock_getter1, args=(self.dir, "id1", qs2m, qm2s)) p.start() r = qs2m.get() self.assertFalse(r, "Slave process was able to get lock!") l.release() d = qs2m.get() got_lock = l.acquire_if_available() self.assertFalse(got_lock) qm2s.put(True) l.acquire() l.release() l.cleanup()
def test_sequential_locking(self): l = Lock(self.dir, "id1") self.assertFalse(l.is_locked()) l.acquire() self.assertTrue(l.is_locked()) l.release() self.assertFalse(l.is_locked()) got_lock = l.acquire_if_available() self.assertTrue(got_lock) got_exception = False try: l.acquire() except LockError: got_exception = True self.assertTrue(got_exception) l.release() l.cleanup() self.assertFalse(os.path.exists(l.path))
def test_self_deadlock(self): l1 = Lock(self.dir, "id1") l2 = Lock(self.dir, "id2") l1.acquire() l2.acquire() l1.release() l2.release() l1.acquire() l2.acquire() l2.release() l1.release()