def test_multiple_parallel(self): """ Try to cancel multiple running futures in parallel """ random.seed(0) self.executor = ParallelThreadPoolExecutor() # Put several task with random sets fs = [] self.called = 0 for i in range(10): f = CancellableFuture() f.task_canceller = self._canceller f._must_stop = threading.Event() r_letter1, r_letter2 = random.choice('abcxyz'), random.choice('abcxyz') f = self.executor.submitf({r_letter1, r_letter2}, f, self._cancellable_task, f, 2) f.add_done_callback(self._on_end_task) fs.append(f) time.sleep(10 * 2 + 1) # in the worst case, there is a dependency between every task, so 2*10 self.assertEqual(self.called, 10) for f in fs: self.assertIsInstance(f.result(), int) self.assertTrue(f.done())