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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:07:11+00:00 2026-05-29T16:07:11+00:00

I think I am following the python documentation correctly but I am having trouble

  • 0

I think I am following the python documentation correctly but I am having trouble getting the result I am looking for. I basically have a list of numbers, that are being passed to a function of nested for loops and the output is saved in a dictionary.

Here’s the code:

from multiprocessing import Pool, Manager

list = [1,2,3,10]
dictionary = {}
def test(x, dictionary):
    for xx in range(100):
        for xxx in range(100):
            dictionary[x]=xx*xxx



if __name__ == '__main__':
    pool = Pool(processes=4)
    mgr = Manager()
    d = mgr.dict()
    for N in list:
        pool.apply_async(test, (N, d))

    # Mark pool as closed -- no more tasks can be added.
    pool.close()

    # Wait for tasks to exit
    pool.join()

    # Output results
    print d

Here’s the expected result:

{1: 9801, 2: 9801, 3: 9801, 10: 9801}

Any suggestions of what I’m doing wrong? Also, I haven’t convinced myself that shared resources are the best approach(thinking of using a database to maintain state) so if my approach is completely flawed or there’s a better way to do this in python please let me know.

  • 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-29T16:07:17+00:00Added an answer on May 29, 2026 at 4:07 pm

    Change the definition of test to:

    def test(x, d):
        for xx in range(100):
            for xxx in range(100):
                d[x]=xx*xxx
    

    Otherwise you’re just incrementing some global dictionary (without synchronization) and never access it later.


    As for the general approach, I think this one in particular has a lot of contention on the shared dictionary. Do you really have to update it from each process as soon as that? Accumulating batches of partial results in each process and just updating the shared object once in a while should perform better.

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

Sidebar

Related Questions

I have been having the following problem, i think it's probably due to the
I think I am following the right approach but I am still getting an
Given I have two File objects I can think of the following implementation: public
I have the following situation I think would be best to show in sample
I would like to do the following but I don't think this will work:
I'm looking for information on how to read GnuCash files using python. I have
I have the following python modules. Sorry if the code is ugly. This is
I have been learning Python by following some pygame tutorials . Therein I found
I have the following code in Python: def point_to_index(point): if point not in points:
Consider the two following Python code examples, which achieves the same but with significant

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.