The interface for performing tasks utilizing a pool of threads or processes is provided by this encapsulation layer on top of Python’s multitasking and multithreading components. The concurrent.futures module of Python came up with an abstract class “Executor” along with its concrete subclasses that are being used in place of the “Executor” class. It’s because the “Executor” class cannot be utilized directly in the code.”
Example 01
Let’s get started with the first illustration of our article. We will be showing you the use of the ProcessPoolExecutor subclass of “Executor” in this illustration, i.e., receive a stream of tasks for completing the jobs since it employs multi-processing. That stream allocates jobs to existing threads and plans their execution. We have started this python program with the use of the “concurrent.futures” module importing its ProcessPoolExecutor subclass here in the code.
After that, we have been importing the sleep class of a time module in python via the use of from and import keywords. We are defining a function with the name “Thread,” taking a message variable as an argument in it. This function has been utilizing the function call to sleep() function of python to make the execution halt for 2 seconds only and return the message to the main() function.
While the main() function definition has been started with the call to the “ProcessPoolExecutor” function of concurrent.futures module to create a pool of 5 processes and save the pool result to an object “executor”. This “Executor” object has been used to call the “submit” function to create 5 threads by passing them a message “Completed” in the parameters. The function execution tried to sleep for 2 seconds and return the message. The returned result would be saved in the variable “future”. We have been calling the “done” function with the future object variable within the “print” statement of the python function to display the returned result, i.e., true or false.
After this, we have been using the sleep function to make the execution of the program continue after 5 seconds and then check the result of the executor pool with the use of the done function of the future object. In the end, the “future” object has been used to call the result() built-in function of Python concurrent. Futures module to see the actual result, i.e., Completed at the end.
In the last lines, we can see that the main() function has been called here. Let’s just save and execute our code to see the actual result for using the concurrent.futures packages in python programs. The image below includes the code for the aforementioned explanation as well.
After running this program in Spyder 3, we have got the shown-below result in the Spyder3 tool. When the Executor pool of 5 threads has been started, it calls the Thread function and sleeps for 2 seconds, and then returns the message. As the pool process doesn’t complete itself yet, so the “future.done” returns False, and we have displayed the “False” result. The execution of this program sleeps for the next 5 seconds and then displays the result of “future.done” again.
As a total of 5 seconds are passed, and the pool has been executed successfully, it returns true in this case. At last, the “future.result” function displayed the message “Completed” for the Pool of 5 processes’ complete execution. This is the expected outcome of the above-affixed code.
Example 02
Let’s take a look at another example to use the ProcessPoolExecutor subclass of concurrent.futures module in python. So, we have imported the subclass ProcessPoolExecutor here. After this, we have initialized a list “Num” of 4 different numerical values, i.e., 14, 7, 3, 15. We have been using a total of 2 functions in this program. The main() function execution started with the use of the ProcessPoolExecutor context manager.
Context manager may be used as a second method of creating ProcessPoolExecutor instances. It functions similarly to the approach shown in the preceding example. The context manager’s best feature is how syntactically appealing it is. So, we have been using it here to create a pool of 3, i.e., total workers. With this, the executor has been using the map() function to pass a number of each element of a “Num” list as a parameter to the “cube” function for mapping. The cube function will be returning the cube of each number passed from the list to it via the use of “n*n*n” formulae and save the returned result to variable “r”.
The “for” loop is here to use the “r” result for each element to display each returned value from the cube function on the console via the use of the “print” function with the “val” variable. The last 2 lines show the use of the main() function call. The image below includes the code for the aforementioned explanation as well.
The output has been showing the cube of all the 4 elements of the “Num” list. This is the expected outcome of the above-affixed code.
Conclusion
This article is all about the use of the “concurrent.futures” module of Python in the programs to see its uses. Within our first example, we have utilized it to create a pool of 5 processes and return the result before and after the sleep function usage in the code. After this, we utilized another example to consume the “concurrent.futures” module and display some mathematical calculations on our Sypder tool Python tool’s console.