code: def main(_):
      assert FLAGS.num_instances
      assert FLAGS.name
      zone = default_zone()
      assert zone
  
      code_dir = None
      if FLAGS.code_dir:
          code_dir = os.path.abspath(os.path.expanduser(FLAGS.code_dir))
  
      # Suffixes per instance
      if FLAGS.per_instance_suffix_file:
          with tf.gfile.Open(FLAGS.per_instance_suffix_file) as f:
              suffixes = [l.strip() for l in f.readlines()]
      else:
          suffixes = list(range(FLAGS.num_instances))
      assert len(suffixes) == FLAGS.num_instances
  
      vm_info = list_vm_names_and_ips()
      vm_names = list(zip(*vm_info))[0] if vm_info else []
  
      pool = mp.Pool(FLAGS.num_threads)
      async_results = []
  
      assert FLAGS.log_dir
      log_dir = os.path.join(FLAGS.log_dir, FLAGS.name)
      tf.gfile.MakeDirs(log_dir)
      assert log_dir.startswith("gs://")
      if not log_dir.endswith("/"):
          log_dir += "/"
      # Write a test file to make sure gcloud GCS APIs are enabled
      test_filename = os.path.join(log_dir, "check_write")
      with tf.gfile.Open(test_filename, "w") as f:
          f.write("testing GCS write")
      tf.gfile.Remove(test_filename)
  
      instance_ids = list(range(FLAGS.num_instances))
      if FLAGS.instance_ids:
          instance_ids = [int(i) for i in FLAGS.instance_ids.split(",")]
      tf.logging.info("Launching %d instances", len(instance_ids))
  
      for i in instance_ids:
          instance_name = "%s-%d" % (FLAGS.name, i)
          existing_ip = (
              vm_info[vm_names.index(instance_name)][1]
              if instance_name in vm_names
              else None
          )
          logging = (
              LOGS.format(task_id=i, bucket=log_dir) if log_dir else ""
          )
          delete = DELETE_SELF.format(zone=zone)
          if FLAGS.debug_keep_up:
              assert len(instance_ids) == 1
              delete = ""
          command = "{prefix} {suffix} {logging}; {delete}".format(
              prefix=FLAGS.command_prefix,
              suffix=suffixes[i],
              delete=delete,
              logging=logging,
          )
          args = (
              instance_name,
              command,
              existing_ip,
              FLAGS.cpu,
              FLAGS.mem,
              code_dir,
              FLAGS.setup_command,
          )
          res = pool.apply_async(launch_instance, args)
          async_results.append((res, instance_name, i))
  
      failed = []
      for res, instance_name, i in async_results:
          try:
              res.get()
          except Exception as e:  # pylint: disable=broad-except
              failed.append((instance_name, i))
              tf.logging.error(
                  "Failed to launch task %s due to exception %s",
                  instance_name,
                  str(e),
              )
  
      results = []
      if failed:
          ids_for_flag = ",".join(
              [str(i) for i in list(zip(*failed))[1]]
          )
          tf.logging.error(
              "Failed to launch %d jobs. Tasks: %s. "
              "Attempting delete in case they are still up. Rerun with "
              "--instance_ids='%s' to attempt relaunch.",
              len(failed),
              str(failed),
              ids_for_flag,
          )
          for instance_name, _ in failed:
              res = pool.apply_async(delete_instance, (instance_name,))
              results.append(res)
  
      for res in results:
          try:
              res.get()
          except:  # pylint: disable=bare-except
              pass
  
      tf.logging.info("Launching complete.")
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import contextlib
  import multiprocessing
  import os
  import socket
  import subprocess
  import time
  from tensor2tensor.utils import cloud_mlengine
  import tensorflow.compat.v1
label_desc: Use image node_id 10 for calling a global function with example usage: main(_) without return types