Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:39:02+00:00 2026-06-10T05:39:02+00:00

I am confused with Python multiprocessing. I am trying to speed up a function

  • 0

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:

  1. Why does apply_async takes longer than the “normal way” ?
  2. What I am doing wrong with map?
  3. Does it makes sense to speed up such tasks with multiprocessing at all?
  4. Finally: after all I have read here, I am wondering if multiprocessing in python works on windows at all ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:39:03+00:00Added an answer on June 10, 2026 at 5:39 am

    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.

     # METHOD 2 : APPLY_ASYNC
     jobs = 4
     size = len(List1)
     pool = Pool(4)
     results = []
     # split the list into 4 equally sized chunks and submit those to the pool
     heads = range(size/jobs, size, size/jobs) + [size]
     tails = range(0,size,size/jobs)
     for tail,head in zip(tails, heads):
          werte = pool.apply_async(foo, args=(List1[tail:head],))
          results.append(werte)
    
     pool.close()
     pool.join() # wait for the pool to be done
    
     for result in results:
          werte = result.get() # get the return value from the sub jobs
    

    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 foo to EVERY element in List1 in a separate process, this will take quite a while. So if you’re pool has 4 processes map will 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 to foo where as apply_async takes a slice of the list. Try the following code

    def foo(thing):
        print thing
    
    map(foo, ['a','b','c','d'])
    

    That’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 chunksize argument to map to specify an approximate size of for each chunk.

    pool.map(foo, List1, chunksize=size/jobs) 
    

    I don’t know though if there is a problem with map on 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never code in Python and trying to switch from Javascrpt/SVG. Being confused
I am trying to write this loop in Python but get confused. Basically I
I'm pretty new to Python and am completely confused by .join() which I have
I'm confused with file moving under python. Under windows commandline, if i have directory
I am currently working with Python and have been confused over the fact that
I have a concern about multiprocessing.Manager() in python. Here is the example: import multiprocessing
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
I'm just learning python and confused when a def of a function ends? I
A python docstring must be given as a literal string; but sometimes it's useful
I'm new to python, and confused by certain behavior. I have a directory called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.