How to do it...

Here is an example of thread and process pool usage, where we will compare the execution time with the time it takes for sequential execution.

The task to be performed is as follows: we have a list of 10 elements. Each element of the list is made to count up to 100,000,000 (just to waste time), and then the last number is multiplied by the i-th element of the list. In particular, we are evaluating the following cases:

  • Sequential execution
  • Thread pool with five workers
  • Process pool with five workers

Now, let's look at how to do it:

  1. Import the relevant libraries:
import concurrent.futures
import time
  1. Define the list of numbers from 1 to 10:
number_list = list(range(1, 11))
  1. The count(number) function counts the numbers from 1 to 100000000, and then returns the product of number × 100,000,000:
def count(number):
for i in range(0,100000000):
i += 1
return i*number
  1. The evaluate(item) function evaluates the count function on the item parameter. It prints out the item value and the result of count(item):
def evaluate(item):
result_item = count(item)
print('Item %s, result %s' % (item, result_item))
  1. In __main__, the sequential execution, thread pool, and process pool are executed:
if __name__ == '__main__':
  1. For the sequential execution, the evaluate function is executed for each item of number_list. Then, the execution time is printed out:
    start_time = time.clock()
for item in number_list:
evaluate(item)
print('Sequential Execution in %s seconds' % (time.clock() -
start_time))
  1. Regarding thread and process pool execution, the same number of workers (max_workers=5) is used. Of course, for both pools, execution times are displayed:
    start_time = time.clock()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as
executor:
for item in number_list:
executor.submit(evaluate, item)
print('Thread Pool Execution in %s seconds' % (time.clock() -
start_time))
start_time = time.clock()
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as
executor:
for item in number_list:
executor.submit(evaluate, item)
print('Process Pool Execution in %s seconds' % (time.clock() -
start_time))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset