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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:52:08+00:00 2026-06-11T14:52:08+00:00

I have a script that includes opening a file from a list and then

  • 0

I have a script that includes opening a file from a list and then doing something to the text within that file. I’m using python multiprocessing and Pool to try to parallelize this operation. A abstraction of the script is below:

import os
from multiprocessing import Pool

results = []
def testFunc(files):
    for file in files:
        print "Working in Process #%d" % (os.getpid())
        #This is just an illustration of some logic. This is not what I'm actually doing.
        for line in file:
            if 'dog' in line:
                results.append(line)

if __name__=="__main__":
    p = Pool(processes=2)
    files = ['/path/to/file1.txt', '/path/to/file2.txt']
    results = p.apply_async(testFunc, args = (files,))
    results2 = results.get()

When I run this the print out of the process id is the same for each iteration. Basically what I’m trying to do is take each element of the input list and fork it out to a separate process, but it seems like one process is doing all of the work.

  • 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-11T14:52:09+00:00Added an answer on June 11, 2026 at 2:52 pm
    • apply_async farms out one task to the pool. You would need to call
      apply_async many times to exercise more processors.
    • Don’t allow both processes to try to write to the same list,
      results. Since the pool workers are separate processes, the two
      won’t be writing to the same list. One way to work around this is to use an ouput Queue. You could set it up yourself, or use apply_async‘s callback to setup the Queue for you. apply_async will call the callback once the function completes.
    • You could use map_async instead of apply_async, but then you’d
      get a list of lists, which you’d then have to flatten.

    So, perhaps try instead something like:

    import os
    import multiprocessing as mp
    
    results = []   
    
    def testFunc(file):
        result = []
        print "Working in Process #%d" % (os.getpid())
        # This is just an illustration of some logic. This is not what I'm
        # actually doing.
        with open(file, 'r') as f:
            for line in f:
                if 'dog' in line:
                    result.append(line)
        return result
    
    
    def collect_results(result):
        results.extend(result)
    
    if __name__ == "__main__":
        p = mp.Pool(processes=2)
        files = ['/path/to/file1.txt', '/path/to/file2.txt']
        for f in files:
            p.apply_async(testFunc, args=(f, ), callback=collect_results)
        p.close()
        p.join()
        print(results)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Within my HTML, I have a php script that includes a file. At that
I have a PHP script (index.php) that includes an index.html file in a sub-directory
I have a HTML that includes a Javascript file. This script contains a special
I have a script that includes a config file (called accountsconfig.php) that contains a
I have a msbuild script that includes creation of a few PDF's using PDF
I have a PHP script that includes another file in an adjacent directory. Example
I have a page that includes a dynamic js-script depending on the page I'm
I have script that reads remote file content and writes it to local server.
I have a script that is designed to parse XML postbacks from Ultracart, right
So I have a script that includes forms that are stored in files named

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.