Starts intermediate folder sync which copies files from 'opt/ml/output/intermediate'
    directory to the provided s3 output location as files created or modified.
    If files are deleted it doesn't delete them from s3.

    It starts intermediate folder behavior as a daemonic process and
    only if the directory doesn't exists yet, if it does - it indicates
    that platform is taking care of syncing files to S3 and container should not interfere.

    Args:
        s3_output_location (str): name of the script or module.
        region (str): the location of the module.

    Returns:
        (multiprocessing.Process): the intermediate output sync daemonic process.

def start_sync(s3_output_location, region):
    """Starts intermediate folder sync which copies files from 'opt/ml/output/intermediate'
    directory to the provided s3 output location as files created or modified.
    If files are deleted it doesn't delete them from s3.

    It starts intermediate folder behavior as a daemonic process and
    only if the directory doesn't exists yet, if it does - it indicates
    that platform is taking care of syncing files to S3 and container should not interfere.

    Args:
        s3_output_location (str): name of the script or module.
        region (str): the location of the module.

    Returns:
        (multiprocessing.Process): the intermediate output sync daemonic process.
    """
    if not s3_output_location or os.path.exists(intermediate_path):
        logger.debug('Could not initialize intermediate folder sync to s3.')
        return

    # create intermediate and intermediate_tmp directories
    os.makedirs(intermediate_path)
    os.makedirs(tmp_dir_path)

    # configure unique s3 output location similar to how SageMaker platform does it
    # or link it to the local output directory
    url = urlparse(s3_output_location)
    if url.scheme == 'file':
        logger.debug('Local directory is used for output. No need to sync any intermediate output.')
        return
    elif url.scheme != 's3':
        raise ValueError("Expecting 's3' scheme, got: %s in %s" % (url.scheme, url))

    # create s3 transfer client
    client = boto3.client('s3', region)
    s3_transfer = s3transfer.S3Transfer(client)
    s3_uploader = {
        'transfer': s3_transfer,
        'bucket': url.netloc,
        'key_prefix': os.path.join(url.path.lstrip('/'), os.environ.get('TRAINING_JOB_NAME', ''),
                                   'output', 'intermediate'),
    }

    # Add intermediate folder to the watch list
    inotify = inotify_simple.INotify()
    watch_flags = inotify_simple.flags.CLOSE_WRITE | inotify_simple.flags.CREATE
    watchers = {}
    wd = inotify.add_watch(intermediate_path, watch_flags)
    watchers[wd] = ''

    # start subprocess to sync any files from intermediate folder to s3
    p = multiprocessing.Process(target=_watch, args=[inotify, watchers, watch_flags, s3_uploader])
    # Make the process daemonic as a safety switch to prevent training job from hanging forever
    # in case if something goes wrong and main container process exits in an unexpected way
    p.daemon = True
    p.start()
    return p