code: def write(self, fname: str) -> None:
      dname, bname = os.path.split(fname)
      f = tempfile.NamedTemporaryFile(
          prefix=bname, dir=dname, mode="w", delete=False
      )
      try:
          tmp_filename = f.name
          with f:
              data = []
              if self._sock_port:
                  data.append(f"{self.SOCK_TOKEN}{self._sock_port}")
              data.append(self.EOF_TOKEN)
              port_str = "\n".join(data)
              written = f.write(port_str)
              assert written == len(port_str)
          os.rename(tmp_filename, fname)
      except Exception:
          os.unlink(tmp_filename)
          raise
  
file_path: wandb/wandb/sdk/service/port_file.py
imports: import os
  import tempfile
  from typing import Optional
label_desc: Use image node_id 2 for calling the PortFile obj's underlying member method code with example usage: obj.write(fname) without return types