code: def lock_pidfile_or_die(pidfile):
      
      pid = os.getpid()
      try:
          remove_if_stale_pidfile(pidfile)
          pid_write_file = pidfile + '.' + str(pid)
          fpid = open(pid_write_file, 'w')
          try:
              fpid.write("%s\n" % pid)
          finally:
              fpid.close()
          if not take_file_lock(pid_write_file, pidfile, "%s\n" % pid):
              sys.exit(1)
      except SystemExit:
          raise
      except Exception:
          log.exception("unable to take pidfile")
          sys.exit(1)
      return pid
docs: @pidfile:
          must be a writable path
  
      Exceptions are logged.
  
      Returns the PID.