In Python, ThreadPoolExecutor and ProcessPoolExecutor, subclasses of Executor, are easy-to-use modules for multitasking.
They are easy-to-use because we only need to submit a task, consisting of a function and its parameters, to the executor then the executor will run the tasks synchronously for us.
For example,
from concurrent.futures import ThreadPoolExecutor
def say_something(var):
print(var)
pool = ThreadPoolExecutor(2)
pool.submit(say_something, 'hello')
pool.submit(say_something, 'hi')
Output (the order maybe different):
hello
hi
However, the function may have multiple parameters.
In Python,
ThreadPoolExecutor
andProcessPoolExecutor
, subclasses ofExecutor
, are easy-to-use modules for multitasking.They are easy-to-use because we only need to submit a task, consisting of a function and its parameters, to the executor then the executor will run the tasks synchronously for us. For example,
Output (the order maybe different):
However, the function may have multiple parameters.
For such cases, we can use
lambda
to tackle the trick.Output: