How to use ThreadPoolExecutor to call a Function Asynchronously in Python
The ThreadPoolExecutor class from the concurrent.futures modules in Python, helps to run parallel tasks. ThreadPoolExecutor is a subclass of Executor which is an abstract class that provides functions to run task asynchronously. ThreadPoolExecutor uses a pool of threads to execute tasks asynchronously. In order to signal threads to exit gracefully, exceptions in the main thread need to be caught and handled. Therefore, it is recommended that ThreadPoolExecutor not to be used for long running-task.
ThreadPoolExecutor can call a function with and without parameters. The following is an example Python code to process a task asynchronously using the ThreadPoolExecutor class:
import concurrent.futures from time import sleep api_names = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"] def call_api_func(api): print("Calling API : ", api) sleep(10) return True # number of threads to run at one time NUMBER_OF_THREADS = 5 with concurrent.futures.ThreadPoolExecutor(NUMBER_OF_THREADS) as executor: results = [executor.submit(call_api_func, api) for api in api_names] for f in concurrent.futures.as_completed(results): print("complete! result:", f.result())