def test_execution_pipeline(self): executor, server = self._start_components([test_utils.TaskOneReturn]) self.assertEqual(0, executor.wait_for_workers(timeout=WAIT_TIMEOUT)) t = test_utils.TaskOneReturn() progress_callback = lambda *args, **kwargs: None f = executor.execute_task(t, uuidutils.generate_uuid(), {}, progress_callback=progress_callback) waiters.wait_for_any([f]) event, result = f.result() self.assertEqual(1, result) self.assertEqual(base_executor.EXECUTED, event)
def test_execution_failure_pipeline(self): task_classes = [ test_utils.TaskWithFailure, ] executor, server = self._start_components(task_classes) t = test_utils.TaskWithFailure() progress_callback = lambda *args, **kwargs: None f = executor.execute_task(t, uuidutils.generate_uuid(), {}, progress_callback=progress_callback) waiters.wait_for_any([f]) action, result = f.result() self.assertIsInstance(result, failure.Failure) self.assertEqual(RuntimeError, result.check(RuntimeError)) self.assertEqual(base_executor.EXECUTED, action)
def do_while_futures_modify(futures, fn, *args, **kwargs): """Do while to execute a function upon completion from a collection Will execute the specified function with its arguments when one of the futures from the passed collection finishes. Additionally, the future is passed as first argument to the function. Modifies the collection by removing completed elements, :param futures: list, set or dictionary of futures :type futures: list :py:class:`futurist.GreenFuture` :param fn: function to execute upon the future finishing exection :param args: arguments for the function :param kwargs: amount of arguments for the function """ waits = waiters.wait_for_any(futures) while len(waits[0]) > 0 or len(waits[1]) > 0: for future in waiters.wait_for_any(futures)[0]: fn(future, *args, **kwargs) futures.remove(future) waits = waiters.wait_for_any(futures)
def test_wait_for_any(self): fs = [] for _i in range(0, 10): fs.append(self.executor.submit( mini_delay, use_eventlet_sleep=self.use_eventlet_sleep)) all_done_fs = [] total_fs = len(fs) while len(all_done_fs) != total_fs: done, not_done = waiters.wait_for_any(fs) all_done_fs.extend(done) fs = not_done self.assertEqual(total_fs, sum(f.result() for f in all_done_fs))
def test_wait_for_any(self): fs = [] for _i in range(0, 10): fs.append( self.executor.submit( mini_delay, use_eventlet_sleep=self.use_eventlet_sleep)) all_done_fs = [] total_fs = len(fs) while len(all_done_fs) != total_fs: done, not_done = waiters.wait_for_any(fs) all_done_fs.extend(done) fs = not_done self.assertEqual(total_fs, sum(f.result() for f in all_done_fs))
def main(): # the behavior is specific to GreenThreadPoolExecutor threadpool = futurist.ThreadPoolExecutor(max_workers=4) rrwlock = ReentrantReadWriteLock() futures = [] futures.append(threadpool.submit(get_write, rrwlock)) futures.append(threadpool.submit(get_read, rrwlock)) # Get the results and verify only one of the calls succeeded # assert that the other call is still pending results = waiters.wait_for_any(futures) # these statements will be printed print(results[0].pop().result == True) print(len(results[1]))
def main(): # the behavior is specific to GreenThreadPoolExecutor threadpool = futurist.GreenThreadPoolExecutor(max_workers=4) rrwlock = ReentrantReadWriteLock() futures = [] futures.append(threadpool.submit(get_write, rrwlock)) futures.append(threadpool.submit(get_read, rrwlock)) # Get the results and verify only one of the calls succeeded # assert that the other call is still pending # this call will block indefinitely, it should not block state of threads # in threadpool should not be able to cause wait_for_any to block # indefinitely. results = waiters.wait_for_any(futures) # these statements will never be reached print(results[0].pop().result == True) print(len(results[1]))