Esempio n. 1
0
 def _write(self, pid_file):
     try:
         pid_file.seek(0)
         pid_file.write('{}\n'.format(os.getpid()))
         pid_file.close()
     except IOError as e:
         raise exc.PidFileError(self.path) from e
Esempio n. 2
0
 def release(self):
     with self._open() as pid_file:
         self._ensure_owning(pid_file)
         try:
             os.unlink(self.path)
         except OSError:
             raise exc.PidFileError(self.path) from e
Esempio n. 3
0
 def _read(self, pid_file):
     pid = None
     try:
         raw = pid_file.read(1024)
         if raw:
             pid = int(raw, 10)
     except (IOError, ValueError) as e:
         raise exc.PidFileError(self.path) from e
     return pid
Esempio n. 4
0
    def ping_proc(self, pid):
        try:
            os.kill(pid, 0)
            return True
        except OSError as e:
            if e.errno != errno.ESRCH:
                raise exc.PidFileError(self.path) from e

        return False
Esempio n. 5
0
    def _open(self):
        for attempt in range(5):
            pid_fd = None
            try:
                pid_fd = os.open(self.path, os.O_RDWR | os.O_CREAT)
                fcntl.flock(pid_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except OSError as e:
                if pid_fd is not None:
                    os.close(pid_fd)
                if e.errno != errno.EWOULDBLOCK:
                    raise exc.PidFileError(self.path) from e

                time.sleep(0.1)
                continue

            break
        else:
            raise exc.PidFileLockError(self.path)

        return os.fdopen(pid_fd, 'w+t')