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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:01:29+00:00 2026-06-11T19:01:29+00:00

I wrote a function to modify a passed in dictionary. However, when I parallelized

  • 0

I wrote a function to modify a passed in dictionary. However, when I parallelized the code using the multiprocessing module, it exhibits different behavior then when run in serial. The dictionary is not modified.

Attached below is a toy example of my issue. The dictionary is not modified when run using map_async, but is modified when run in a for loop. Thanks for clarifying my confusion!

#!/usr/bin/env python

from multiprocessing import Pool

def main1(x):
  x['a'] = 1
  print x

  return 1

def main2(x):
  x['b'] = 2
  print x

p = Pool(2)
d = {1:{}, 2:{}}
r = p.map_async(main1, d.values())
print r.get()
print "main1", d

for x in d.values():
  main2(x)

print "main2", d
  • 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-11T19:01:30+00:00Added an answer on June 11, 2026 at 7:01 pm

    r = p.map_async(main1, d.values()) does this:

    1) Evaluate d.values() – that’s [{}, {}]
    2) Execute main1(item) for each item in that list on a worker from the pool
    3) Gather the results from those calls into a list – [1, 1] – because that’s what main1 returns
    4) Assign that list to r

    So it does exactly what the builtin function map() does, but in a parallelized way.

    This means, your dict d never makes it into any of the worker processes, because it’s not a reference to d that’s passed to map_async, and therefore main1.

    And even if you would pass in a reference to d – it wouldn’t work for the reasons explained by @Roland Smith.

    The point is: You shouldn’t modify the dictionary in the first place. It’s not even very good style in conventional programming for functions to modify their arguments, even if they can. For parallel programming it’s absolutely crucial to follow a functional programming style, which in this context means:

    Functions should do the computation on their input, and return a result that is further processed.

    The functions map and reduce are very common in functional programming, and combined together they form a pattern that is suited very well for distributed computing. From the Wikipedia article on MapReduce:

    “Map” step: The master node takes the input, divides it into smaller
    sub-problems, and distributes them to worker nodes. A worker node may
    do this again in turn, leading to a multi-level tree structure. The
    worker node processes the smaller problem, and passes the answer back
    to its master node.

    “Reduce” step: The master node then collects the answers to all the
    sub-problems and combines them in some way to form the output – the
    answer to the problem it was originally trying to solve.

    So in order to effectively parallelize your program it helps to try to think of your problem in terms of those functions.

    For a very concrete example, see the Article The Trouble With Multicore in IEEE Spectrum. It describes a method of parallelizing the computation of PI that could easily be implemented with map/reduce.

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

Sidebar

Related Questions

I wrote a function to zoom in a UIView using CGAffineTransformScale. After a 2x
I wrote a function in bash script. However, it's complaining about syntax. I really
I wrote a WCF service with a function that using absolute path to get
I wrote a code that switches an image from Yes to No using jQuery.
I wrote recursive PHP function for folder deletion. I wonder, how do I modify
I have the following php function which i wrote a while ago. Now however,
I wrote the following html file <code> <html> <head> <script text =text/javascript> function createCookie(name,value,days)
I wrote function onclick of a element <div class=left_collapsed>Որոնում</div> <div class=container style=display: none;> <ul>
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map
I wrote a function in Java to fetch 10 links by their IDs from

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.