code: def pick_unused_ports(
      num_ports, retry_interval_secs=1, retry_attempts=5
  ):
      """Reserves and returns a list of `num_ports` unused ports."""
      if num_ports <= 0:
          raise ValueError(
              "Number of ports, must be >= 1, got: %s" % num_ports
          )
      ports = set()
      for _ in range(retry_attempts):
          ports.update(
              portpicker.pick_unused_port()
              for _ in range(num_ports - len(ports))
          )
          ports.discard(None)  # portpicker returns None on error.
          if len(ports) == num_ports:
              return list(ports)
          # Duplicate ports can be returned, especially when insufficient ports are
          # free. Wait for more ports to be freed and retry.
          time.sleep(retry_interval_secs)
  
      # Could not obtain enough ports. Release what we do have.
      return_ports(ports)
  
      raise RuntimeError(
          "Unable to obtain %d unused ports." % num_ports
      )
  
imports: import time
  import portpicker
label_desc: Use image node_id 1 for calling a global function with example usage: pick_unused_ports(num_ports, retry_interval_secs, retry_attempts) and returns: list