def convert_shard(shard, shard_name_fn, example_fn):
  """Multithreading helper to serialize an individual shard to disk.

  Args:
    shard: Tuple of shard id (int) and examples (ImageMetadata list).
    shard_name_fn: Maps shard id to file name.
    example_fn: Maps ImageMetadata to Example proto.
  """
  shard_id, examples = shard

  # The example might have an "objects" attribute, which in this case, points
  # to the filename of a HDF5 file that should be opened. Many examples share
  # the same objects HDF5 file for storing their object data, so we keep the
  # file handle open until we hit an example that points to a different file.
  maybe_open_file = hasattr(examples[0], "objects")
  if maybe_open_file:
    current_file = examples[0].objects
    current_file_handle = h5py.File(current_file, "r")

  output_file = shard_name_fn(shard_id)
  tf.logging.info("Writing shard %s" % output_file)
  with tf.io.TFRecordWriter(output_file) as writer:
    for example in examples:
      # Check if the HDF5 file should be updated.
      if maybe_open_file:
        if example.objects != current_file:
          current_file_handle.close()
          current_file = example.objects
          current_file_handle = h5py.File(current_file, "r")
        example.objects = current_file_handle
      example_proto = example_fn(example)
      writer.write(example_proto.SerializeToString())
  if maybe_open_file:
    current_file_handle.close()
Multithreading helper to serialize an individual shard to disk.

  Args:
    shard: Tuple of shard id (int) and examples (ImageMetadata list).
    shard_name_fn: Maps shard id to file name.
    example_fn: Maps ImageMetadata to Example proto.
  
language/capwap/utils/io_utils.py