def run(self): try: self.context.pidfile.acquire(timeout=0.1) except (lockfile.AlreadyLocked, lockfile.LockTimeout): lockpid = None if os.path.exists(self.pidfile): lockpid = int(file(self.pidfile).read().strip()) if lockpid and lockpid != os.getpid() and util.kill(lockpid, 0): raise lockfile.AlreadyLocked, "Process %d has %s locked" % (lockpid, self.pidfile) self.context.pidfile.break_lock() try: with self.context: file(self.pidfile, "w").write(str(os.getpid())) self.mainLoop() finally: if self.context.pidfile.is_locked(): self.context.pidfile.release() if os.path.exists(self.pidfile): os.remove(self.pidfile)
def run(self): try: self.context.pidfile.acquire(timeout=0.1) except (lockfile.AlreadyLocked, lockfile.LockTimeout): lockpid = None if os.path.exists(self.pidfile): lockpid = int(file(self.pidfile).read().strip()) if (lockpid and lockpid != os.getpid() and util.kill(lockpid, 0)): raise lockfile.AlreadyLocked, 'Process %d has %s locked' % ( lockpid, self.pidfile) self.context.pidfile.break_lock() try: with self.context: file(self.pidfile, 'w').write(str(os.getpid())) self.mainLoop() finally: if self.context.pidfile.is_locked(): self.context.pidfile.release() if os.path.exists(self.pidfile): os.remove(self.pidfile)
def test_killDoesNotExist(self): self.assertFalse(util.kill(123456789, 0)) # 32K is max pid
def test_killExists(self): self.assertTrue(util.kill(os.getpid(), 0))