"""Logic for executing functions in parallel using flask-executor. Basically it's a ThreadPoolExecutor, but with g object available. """ import concurrent.futures from analytics.api import executor def execute_in_parallel(requests): """Execute requests in parallel. Provided a dict of requests, execute them with the provided id and kwargs. Return the results of these requests in a dict keyed by keys in the requests parameter. """ futures = { executor.submit( request['func'], *request['args'], **request['kwargs'] ): key for (key, request) in requests.items() } concurrent.futures.wait(futures) output = dict(map(lambda f: (futures[f], f.result()), futures)) return output