issue regarding concurrent.futures.ProcessPoolExecutor

26 Views Asked by At

I am learning Multiprocessing in order to use it in project. I can not understand why ProcessPoolExecutor is taking so much time.

Am I doing something wrong in execution or it shouldn't be implemented in such cases. Please help me out.

import concurrent.futures as cf
import time
import math

class trial():

    def __init__(self, x,y):
        self.x= 10
        self.y= 20
        self.nums = [i for i in range(100000)]

    def _f(x):
        zz= x*x - 10
        return zz


    def main(self):


        start = time.perf_counter()

        result10= ([val for val in map(trial._f, self.nums)])
        print (result10)
        finish1 = time.perf_counter()
        print(f'Finished in {round(finish1-start, 2)} second(s)')


        with cf.ProcessPoolExecutor() as executor:
            result11=([val for val in executor.map(trial._f, self.nums)])
            print (result11)
    

        finish = time.perf_counter()
        print(f'Finished in {round(finish-start, 2)} second(s)')

Simple map function is taking Finished in 0.02 second(s). Executor.map is taking Finished in 30 second(s) (HOW and WHY).

0

There are 0 best solutions below