From 5b755dbbff022af0d9c3fd9ef6f9f124c13dd3a0 Mon Sep 17 00:00:00 2001 From: Maxime Lamothe-Brassard Date: Fri, 23 Sep 2022 15:02:33 -0500 Subject: [PATCH] Fix bugs with parallel exec. (#73) --- lcservice/service.py | 6 ++---- tests/test_lifecycle.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lcservice/service.py b/lcservice/service.py index 83c9aae..449e8fe 100644 --- a/lcservice/service.py +++ b/lcservice/service.py @@ -470,8 +470,7 @@ def parallelExec( self, f, objects, timeout = None, maxConcurrent = None ): results = [] with ThreadPoolExecutor( max_workers=maxConcurrent ) as executor: - future = executor.map( lambda o: self._retExecOrExc( f, o, timeout ), objects, timeout = timeout ) - results = future.result() + results = executor.map( lambda o: self._retExecOrExc( f, o, timeout ), objects, timeout = timeout ) return list( results ) def _retExecOrExc( self, f, o, timeout ): @@ -500,8 +499,7 @@ def parallelExecEx( self, f, objects, timeout = None, maxConcurrent = None ): results = [] with ThreadPoolExecutor( max_workers=maxConcurrent ) as executor: - future = executor.map( lambda o: self._retExecOrExcWithKey( f, o, timeout ), objects.items(), timeout = timeout ) - results = future.result() + results = executor.map( lambda o: self._retExecOrExcWithKey( f, o, timeout ), objects.items(), timeout = timeout ) return list( results ) # LC Service Lifecycle Functions diff --git a/tests/test_lifecycle.py b/tests/test_lifecycle.py index 22e4796..8dfc1b2 100644 --- a/tests/test_lifecycle.py +++ b/tests/test_lifecycle.py @@ -178,4 +178,18 @@ def _inc(): time.sleep( 2.1 ) assert( 3 == n ) + svc._onShutdown() + +def test_parallel(): + global n + svc = lcservice.Service( 'test-service', None ) + + n = 0 + def _inc(): + global n + n += 1 + + res = svc.parallelExec( _inc, [ 1, 2, 3, 4 ], 10, 5 ) + assert( 4 == len( res ) ) + svc._onShutdown() \ No newline at end of file