From the course: Complete Guide to Parallel and Concurrent Programming in Python

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Future: Python demo

Future: Python demo

- [Instructor] When you submit a task to an Executor in Python, the submit method returns an instance of the future class, which encapsulates the asynchronous execution of the callable task. The future class has several methods that can be used to check the status of the task's execution, cancel it if needed, and most importantly, it has a method named result, which is used to retrieve the return value after the call has completed. To demonstrate that in action, I'll recreate my interaction with Olivia from the previous video starting with this basic shell of a program. First, I'll import the ThreadPoolExecutor class from the concurrent.futures module. And I'll also import the time module. Then I'll create a new function named how_many_vegetables, and this will serve as our callable task. Within that function, I'll print a message that Olivia is counting vegetables. Then I'll have the function sleep for 3 seconds. And finally, the function will return a value for the number of…

Contents