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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:37:33+00:00 2026-06-14T18:37:33+00:00

I am trying to solve a big numerical problem which involves lots of subproblems,

  • 0

I am trying to solve a big numerical problem which involves lots of subproblems, and I’m using Python’s multiprocessing module (specifically Pool.map) to split up different independent subproblems onto different cores. Each subproblem involves computing lots of sub-subproblems, and I’m trying to effectively memoize these results by storing them to a file if they have not been computed by any process yet, otherwise skip the computation and just read the results from the file.

I’m having concurrency issues with the files: different processes sometimes check to see if a sub-subproblem has been computed yet (by looking for the file where the results would be stored), see that it hasn’t, run the computation, then try to write the results to the same file at the same time. How do I avoid writing collisions like this?

  • 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-14T18:37:34+00:00Added an answer on June 14, 2026 at 6:37 pm

    @GP89 mentioned a good solution. Use a queue to send the writing tasks to a dedicated process that has sole write access to the file. All the other workers have read only access. This will eliminate collisions. Here is an example that uses apply_async, but it will work with map too:

    import multiprocessing as mp
    import time
    
    fn = 'c:/temp/temp.txt'
    
    def worker(arg, q):
        '''stupidly simulates long running process'''
        start = time.clock()
        s = 'this is a test'
        txt = s
        for i in range(200000):
            txt += s 
        done = time.clock() - start
        with open(fn, 'rb') as f:
            size = len(f.read())
        res = 'Process' + str(arg), str(size), done
        q.put(res)
        return res
    
    def listener(q):
        '''listens for messages on the q, writes to file. '''
    
        with open(fn, 'w') as f:
            while 1:
                m = q.get()
                if m == 'kill':
                    f.write('killed')
                    break
                f.write(str(m) + '\n')
                f.flush()
    
    def main():
        #must use Manager queue here, or will not work
        manager = mp.Manager()
        q = manager.Queue()    
        pool = mp.Pool(mp.cpu_count() + 2)
    
        #put listener to work first
        watcher = pool.apply_async(listener, (q,))
    
        #fire off workers
        jobs = []
        for i in range(80):
            job = pool.apply_async(worker, (i, q))
            jobs.append(job)
    
        # collect results from the workers through the pool result queue
        for job in jobs: 
            job.get()
    
        #now we are done, kill the listener
        q.put('kill')
        pool.close()
        pool.join()
    
    if __name__ == "__main__":
       main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big problem using highcharts, because I have been trying for hours
trying to solve a puzzle which i found here: http://zcasper.blogspot.com/2005/10/google-phone-interview.html the goal is to
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
Am trying to solve a labyrinth by DFS, using adj List to represent the
I'm trying to solve a codechef beginner problem - Enormous Input Test . My
I'm trying to solve the following problem in Redis. I have a list that
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I am trying to solve the following problem with Puppet: I have multiple nodes.
after trying to solve the problem without and with help I'm still stuck. My

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.