简体   繁体   中英

Multiprocessing slower than sequential Python 3.5

I'm trying to use some multi-processing with python to increase the speed of some function, I know creating a process have a big cost but my function is taking something arount 3 sec to execute in sequential so I was thinking that some multi-processing could do the trick.

I was wrong (big whoop) the multi-processing seems slower or at least is not faster. To put some context here what I had before the multi-processing:

for i in range(0, 3):
    for j in range(0, 3):
        result = my_function(my_args)

And now what I have with the multi-processing:

pool = Pool()

for i in range(0, 3):
    for j in range(0, 3):
        result = pool.apply_async(my_function,my_args).get()

pool.close()
pool.join()

I replace the real function with the my_function thingy because my function is a pain in the ass to read basically.

So is the workload to small to use multiprocessing or am I doing something wrong?

EDIT:

As some said it was working sequential because I used the get each time where I needed to use it at the end after the join but now I have a random exception that pop from time to time, here the end of the traceback:

File "/home/rtodo/anaconda3/lib/python3.5/multiprocessing/pool.py", line 608, in get raise self._value IndexError: pop index out of range

here's my code corrected

pool = Pool()
for i in range(0, 3):
    for j in range(0, 3):
        neigbhourhood[i][j] = pool.apply_async(my_function,my_args)



for k in range(0, 3):
    for l in range(0, 3):
            neigbhourhood[i][j] = neigbhourhood[i][j].get()

pool.close()

Where neigbhourhood is a 3x3 array.

Edit of the Edit: I changed a bit the code, now the pool close after the get and I don't use join.

There is no (real) difference in the two examples, as .get() will wait for the process to finish. So your multiprocess example will be sequential as well, but it will take more time as it spawns processes on different nodes.

First launch all your processes, and wait for the results after.

pool = Pool()
futures = []
for i in range(0, 3):
    for j in range(0, 3):
        futures.append(pool.apply_async(my_function,my_args))

# You can also make some additional calculation here if you want

#
# Lets see if the multiprocess stuff is finished
for i in futures:
    i.get()


pool.close()
# pool.join() 

You can leave out the .join() as well, because it is not possible that the main process will finish sooner than the child processes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM