code: def write_with_timeout(
      fd, data, timeout_sec
  ):  # pylint: disable=invalid-name
      """Write data to a file descriptor, with timeout.
  
      This function is intended as a helper function for implementations of ProjectAPIHandler
      write_transport. Tested on Linux and OS X. Not tested on Windows.
  
      Parameters
      ----------
      fd : int
          File descriptor to read from. Must be opened in non-blocking mode (e.g. with O_NONBLOCK)
          if timeout_sec is not None.
  
      data : bytes
          Data to write.
  
      timeout_sec : float or None
          If not None, maximum number of seconds to wait before raising IoTimeoutError.
  
      Returns
      -------
      int :
          The number of bytes written to the file descriptor, if any bytes were written. A value
          in [1, len(data)]. If timeout_sec is None, returns the equivalent of os.write(fd, data).
  
      Raises
      ------
      IoTimeoutException :
          When timeout_sec is not None and that number of seconds elapses before any data is read.
      """
      end_time = (
          None
          if timeout_sec is None
          else time.monotonic() + timeout_sec
      )
  
      num_written = 0
      while data:
          try:
              _await_nonblocking_ready([], [fd], end_time=end_time)
          except IoTimeoutError as exc:
              if num_written:
                  return num_written
  
              raise exc
  
          num_written_this_cycle = os.write(fd, data)
  
          if not num_written_this_cycle:
              os.close(fd)
              raise base.TransportClosedError()
  
          data = data[num_written_this_cycle:]
          num_written += num_written_this_cycle
  
      return num_written
  
imports: import abc
  import argparse
  import base64
  import collections
  import enum
  import io
  import json
  import logging
  import os
  import pathlib
  import re
  import select
  import sys
  import time
  import traceback
  import typing
label_desc: Use image node_id 3 for calling a global function with example usage: write_with_timeout(fd, data, timeout_sec) and returns: num_written, num_written