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 6330861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:56:50+00:00 2026-05-24T17:56:50+00:00

I had a strange problem. I have a file of the format: START 1

  • 0

I had a strange problem. I have a file of the format:

START
1
2
STOP
lllllllll
START
3
5
6
STOP

and I want to read the lines between START and STOP as blocks, and use my_f to process each block.

def block_generator(file):

with open(file) as lines:
    for line in lines:
        if line == 'START': 
            block=itertools.takewhile(lambda x:x!='STOP',lines) 
            yield block   

and in my main function I tried to use map() to get the work done. It worked.

blocks=block_generator(file)
map(my_f,blocks)

will actually give me what I want. But when I tried the same thing with multiprocessing.Pool.map(), it gave me an error said takewhile() wanted to take 2 arguments, was given 0.

    blocks=block_generator(file)
    p=multiprocessing.Pool(4) 
    p.map(my_f,blocks)

Is this a bug?

  1. The file have more than 1000000 blocks, each has less than 100 lines.
  2. I accept the answer form untubu.
  3. But maybe I will simple split the file and use n instance of my original script without multiprocessing to processing them then cat the results together. This way you can never be wrong as long as the script works on a small file.
  • 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-24T17:56:51+00:00Added an answer on May 24, 2026 at 5:56 pm

    How about:

    import itertools
    
    def grouper(n, iterable, fillvalue=None):
        # Source: http://docs.python.org/library/itertools.html#recipes
        "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
        return itertools.izip_longest(*[iter(iterable)]*n,fillvalue=fillvalue)
    
    def block_generator(file):
        with open(file) as lines:
            for line in lines:
                if line == 'START': 
                    block=list(itertools.takewhile(lambda x:x!='STOP',lines))
                    yield block
    
    blocks=block_generator(file)
    p=multiprocessing.Pool(4)
    for chunk in grouper(100,blocks,fillvalue=''):
        p.map(my_f,chunk)
    

    Using grouper will limit the amount of the file consumed by p.map. Thus the whole file need not be read into memory (fed into the task queue) at once.


    I claim above that when you call p.map(func,iterator), the entire iterator is consumed immediatedly to fill a task queue. The pool workers then get tasks from the queue and work on the jobs concurrently.

    If you look inside pool.py and trace through the definitions, you will see
    the _handle_tasks thread gets items from self._taskqueue, and enumerates that at once:

             for i, task in enumerate(taskseq):
                 ...
                 put(task)
    

    The conclusion is, the iterator passed to p.map gets consumed at once. There is no waiting for the one task to end before the next task is gotten from the queue.

    As further corroboration, if you run this:

    demonstration code:

    import multiprocessing as mp
    import time
    import logging
    
    def foo(x):
        time.sleep(1)
        return x*x
    
    def blocks():
        for x in range(1000):
            if x%100==0:
                logger.info('Got here')
            yield x
    
    logger=mp.log_to_stderr(logging.DEBUG)
    logger.setLevel(logging.DEBUG) 
    pool=mp.Pool() 
    print pool.map(foo, blocks()) 
    

    You will see the Got here message printed 10 times almost immediately, and then a long pause due to the time.sleep(1) call in foo. This manifestly shows the iterator is fully consumed long before the pool processes gets around to finishing the tasks.

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

Sidebar

Related Questions

I have a rather strange problem, well strange to me. I have a file
I have a strange problem. I have never had it before. When I try
I've had this strange problem come up. I have a RowDataBound event handler for
I know it is a strange question did someone have had a case where
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I have a very strange problem and i don't know what to do about
On our MOSS Enterprise environment, we have a strange problem. From time to time,
I have a very strange problem with a LINQ to Entities query with EF1.
I have a strange problem with a UITextField - I am setting the value
I have a strange problem with my checkbox. In my adapter I have a

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.