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

  • Home
  • SEARCH
  • 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 4103760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:00:00+00:00 2026-05-20T21:00:00+00:00

I’m wondering about the way that python’s Multiprocessing.Pool class works with map, imap, and

  • 0

I’m wondering about the way that python’s Multiprocessing.Pool class works with map, imap, and map_async. My particular problem is that I want to map on an iterator that creates memory-heavy objects, and don’t want all these objects to be generated into memory at the same time. I wanted to see if the various map() functions would wring my iterator dry, or intelligently call the next() function only as child processes slowly advanced, so I hacked up some tests as such:

def g():
  for el in xrange(100):
    print el
    yield el

def f(x):
  time.sleep(1)
  return x*x

if __name__ == '__main__':
  pool = Pool(processes=4)              # start 4 worker processes
  go = g()
  g2 = pool.imap(f, go)
  g2.next()

And so on with map, imap, and map_async. This is the most flagrant example however, as simply calling next() a single time on g2 prints out all my elements from my generator g(), whereas if imap were doing this ‘lazily’ I would expect it to only call go.next() once, and therefore print out only ‘1’.

Can someone clear up what is happening, and if there is some way to have the process pool ‘lazily’ evaluate the iterator as needed?

Thanks,

Gabe

  • 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-05-20T21:00:01+00:00Added an answer on May 20, 2026 at 9:00 pm

    Let’s look at the end of the program first.

    The multiprocessing module uses atexit to call multiprocessing.util._exit_function when your program ends.

    If you remove g2.next(), your program ends quickly.

    The _exit_function eventually calls Pool._terminate_pool. The main thread changes the state of pool._task_handler._state from RUN to TERMINATE. Meanwhile the pool._task_handler thread is looping in Pool._handle_tasks and bails out when it reaches the condition

                if thread._state:
                    debug('task handler found thread._state != RUN')
                    break
    

    (See /usr/lib/python2.6/multiprocessing/pool.py)

    This is what stops the task handler from fully consuming your generator, g(). If you look in Pool._handle_tasks you’ll see

            for i, task in enumerate(taskseq):
                ...
                try:
                    put(task)
                except IOError:
                    debug('could not put task on queue')
                    break
    

    This is the code which consumes your generator. (taskseq is not exactly your generator, but as taskseq is consumed, so is your generator.)

    In contrast, when you call g2.next() the main thread calls IMapIterator.next, and waits when it reaches self._cond.wait(timeout).

    That the main thread is waiting instead of
    calling _exit_function is what allows the task handler thread to run normally, which means fully consuming the generator as it puts tasks in the workers’ inqueue in the Pool._handle_tasks function.

    The bottom line is that all Pool map functions consume the entire iterable that it is given. If you’d like to consume the generator in chunks, you could do this instead:

    import multiprocessing as mp
    import itertools
    import time
    
    
    def g():
        for el in xrange(50):
            print el
            yield el
    
    
    def f(x):
        time.sleep(1)
        return x * x
    
    if __name__ == '__main__':
        pool = mp.Pool(processes=4)              # start 4 worker processes
        go = g()
        result = []
        N = 11
        while True:
            g2 = pool.map(f, itertools.islice(go, N))
            if g2:
                result.extend(g2)
                time.sleep(1)
            else:
                break
        print(result)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT

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.