def execute_impl(img_num: int, partial_func: partial, cores: int, chunksize: int, progress: Progress, msg: str): task_name = f"{msg} {cores}c {chunksize}chs" progress = Progress.ensure_instance(progress, num_steps=img_num, task_name=task_name) indices_list = range(img_num) if multiprocessing_necessary(img_num, cores): with Pool(cores) as pool: for _ in pool.imap(partial_func, indices_list, chunksize=chunksize): progress.update(1, msg) else: for ind in indices_list: partial_func(ind) progress.update(1, msg) progress.mark_complete()
def test_basic_single_step(self): p = Progress(num_steps=2) self.assertFalse(p.is_started()) self.assertFalse(p.is_completed()) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) p.update(msg="do a thing") self.assertTrue(p.is_started()) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.5) p.mark_complete() self.assertTrue(p.is_completed()) self.assertEqual(len(p.progress_history), 3) self.assertEqual(p.completion(), 1.0)
def test_multi_step(self): # Default to estimating a single step p = Progress(num_steps=2) self.assertEqual(p.end_step, 2) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) p.update(msg="Estimate how complex I am") self.assertEqual(p.end_step, 2) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.5) # First step to execute may decide that more steps are required p.add_estimated_steps(10) self.assertEqual(p.end_step, 12) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.083) p.update(steps=2, msg="Do two things") self.assertEqual(len(p.progress_history), 3) self.assertEqual(p.completion(), 0.25) p.update(steps=2, msg="Do two more things") self.assertEqual(len(p.progress_history), 4) self.assertEqual(p.completion(), 0.417) p.update(steps=3, msg="Do three things") self.assertEqual(len(p.progress_history), 5) self.assertEqual(p.completion(), 0.667) p.mark_complete() self.assertEqual(len(p.progress_history), 6) self.assertEqual(p.completion(), 1.0)