I am confused with Python multiprocessing.
I am trying to speed up a function which process strings from a database but I must have misunderstood how multiprocessing works because the function takes longer when given to a pool of workers than with “normal processing”.
Here an example of what I am trying to achieve.
from time import clock, time
from multiprocessing import Pool, freeze_support
from random import choice
def foo(x):
TupWerteMany = []
for i in range(0,len(x)):
TupWerte = []
s = list(x[i][3])
NewValue = choice(s)+choice(s)+choice(s)+choice(s)
TupWerte.append(NewValue)
TupWerte = tuple(TupWerte)
TupWerteMany.append(TupWerte)
return TupWerteMany
if __name__ == '__main__':
start_time = time()
List = [(u'1', u'aa', u'Jacob', u'Emily'),
(u'2', u'bb', u'Ethan', u'Kayla')]
List1 = List*1000000
# METHOD 1 : NORMAL (takes 20 seconds)
x2 = foo(List1)
print x2[1:3]
# METHOD 2 : APPLY_ASYNC (takes 28 seconds)
# pool = Pool(4)
# Werte = pool.apply_async(foo, args=(List1,))
# x2 = Werte.get()
# print '--------'
# print x2[1:3]
# print '--------'
# METHOD 3: MAP (!! DOES NOT WORK !!)
# pool = Pool(4)
# Werte = pool.map(foo, args=(List1,))
# x2 = Werte.get()
# print '--------'
# print x2[1:3]
# print '--------'
print 'Time Elaspse: ', time() - start_time
My questions:
- Why does apply_async takes longer than the “normal way” ?
- What I am doing wrong with map?
- Does it makes sense to speed up such tasks with multiprocessing at all?
- Finally: after all I have read here, I am wondering if multiprocessing in python works on windows at all ?
So your first problem is that there is no actual parallelism happening in
foo(x), you are passing the entire list to the function once.1)
The idea of a process pool is to have many processes doing computations on separate bits of some data.
This will only give you an actual speedup if the time it takes to process each chunk is greater than the time it takes to launch the process, in the case of four processes and four jobs to be done, of course these dynamics change if you’ve got 4 processes and 100 jobs to be done. Remember that you are creating a completely new python interpreter four times, this isn’t free.
2) The problem you have with map is that it applies
footo EVERY element inList1in a separate process, this will take quite a while. So if you’re pool has 4 processesmapwill pop an item of the list four times and send it to a process to be dealt with – wait for process to finish – pop some more stuff of the list – wait for the process to finish. This makes sense only if processing a single item takes a long time, like for instance if every item is a file name pointing to a one gigabyte text file. But as it stands map will just take a single string of the list and pass it tofoowhere asapply_asynctakes a slice of the list. Try the following codeThat’s the built-in python map and will run a single process, but the idea is exactly the same for the multiprocess version.
Added as per J.F.Sebastian’s comment: You can however use the
chunksizeargument tomapto specify an approximate size of for each chunk.I don’t know though if there is a problem with
mapon Windows as I don’t have one available for testing.3) yes, given that your problem is big enough to justify forking out new python interpreters
4) can’t give you a definitive answer on that as it depends on the number of cores/processors etc. but in general it should be fine on Windows.