def write_pid(self, pid=None):
            """Writes the current process id into the pid file, acquiring the lock if needed.

            This must be invoked to actually update the contents of the pidfile.  It is not necessary to invoke
            ``acquire_pid_lock`` before invoking this method.

            This will not return a false positive.  If the method indicates the pidfile was successfully locked
            and acquired, then no other process believes it is the agent.

            @param pid:  The pid to write into the pidfile.  This is only used for testing.  If None, the current
                processes pid is used.
            @type pid: int
            @return:  If the lock was acquired and the pid was successfully written, then returns a function that, when
                invoked, will release the lock.  This function should be used in place of any previous one returned
                by ``acquire_pid_lock``.  IMPORTANT:  Do not release the lock on the pidfile until the agent process
                terminates.
            @rtype: Func
            """
            if self.__locked_fd is None:
                self.acquire_pid_lock()
            else:
                # If we already acquire_pid_lock was already invoked, then we might be using the scheme where a
                # parent process acquired it separately.  To help cover over the fact some POSIX systems do not
                # have a proper flock implementation, try to make sure we really have the lock.
                try:
                    fcntl.flock(self.__locked_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
                    self._log_debug('Refreshed pidfile lock')
                except IOError, e:
                    if (e.errno == errno.EAGAIN) or (e.errno == errno.EACCES):
                        self._log('Write pidfile failed-')
                        return None
                    else:
                        raise e

            if self.__locked_fd is None:
                self._log('Write pidfile failed+')
                return None

            if pid is None:
                pid = os.getpid()

            fp = self.__locked_fd
            fp.truncate()
            # Very important that we terminate this with a newline.
            fp.write('%d locked\n' % pid)
            self._log('Wrote pidfile')

            # make sure that all data is on disk
            fp.flush()
            os.fsync(fp.fileno())

            return self.__release_lock
dataset/ETHPy150Open scalyr/scalyr-agent-2/scalyr_agent/platform_posix.py/PidfileManager.PidWriter.write_pid/original