def wait_for_tasks_to_complete(batch_service_client, job_ids, timeout):
    timeout_expiration = datetime.datetime.now() + timeout
    print("Monitoring all tasks for 'Completed' state, timeout in {}...".format(timeout), end='')
    while datetime.datetime.now() < timeout_expiration:
        print('.', end='')
        sys.stdout.flush()
        for (job_id, _) in job_ids:
            tasks = batch_service_client.task.list(job_id)
            incomplete_tasks = [task for task in tasks if
                                task.state != batchmodels.TaskState.completed]
            if incomplete_tasks:
                break
        if not incomplete_tasks:
            print()
            return True
        else:
            time.sleep(1)
    raise RuntimeError("ERROR: Tasks did not reach 'Completed' state within "
                       "timeout period of " + str(timeout))