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

The Archive Base Latest Questions

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

How do I write to a parent’s dictionary? I’ve given a list of urls

  • 0

How do I write to a parent’s dictionary? I’ve given a list of urls to some children to retrieve, which then need to write to the parent’s dictionary:

from multiprocessing import Pool
import random

parent_dict={}
urls = ['www.yahoo.com','www.google.com','www.microsoft.com','www.apple.com','www.cisco.com']

def workit(url):
    # retrieve the urls, process some stuff and then add that info to parent_dict
    key = random.randrange(1,10) # pretend that this is the variable that we want in parent_dict
    value = random.randrange(11,20)
    parent_dict[key] = value

pool = Pool(processes = 5)
pool.map(workit,urls)


print parent_dict # returns {}}
  • 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-10T18:34:35+00:00Added an answer on June 10, 2026 at 6:34 pm

    The following code is more or less yours, adapted with to deal with passing around the manager.dict that is mentioned in the python multiprocessing docs.

    from multiprocessing import Pool
    from multiprocessing import Manager
    import random
    
    manager = Manager()
    dproxy = manager.dict()
    
    urls = ['www.yahoo.com','www.google.com','www.microsoft.com','www.apple.com','www.cisco.com']
    
    
    
    def f(url):
        shared_dict = dproxy[0]
        key = random.randrange(1,10)
        value = random.randrange(11,20)
        shared_dict[key] = value
        dproxy[0] = shared_dict
    
    if __name__ == '__main__':
        dproxy[0] = {}
        pool = Pool(processes=5)
    
        pool.map(f, urls)
        print dproxy[0]
    

    There are a couple of things to note here that are critical issues:

    1. This does not always create a dictionary with the same length. Thinking what you are probably thinking right now (some keys were overwritten), I changed the dictionary to a list and the shared_dict[key] = value to shared_list.append((key,value)). There is overwriting going on but the program is actually writing a variable number of pairs too. Sometimes it is three, sometimes two, sometimes four. I’ve added the code here for your edification but I would not actually use the code I posted to do anything, ever
    2. This doesn’t behave well when run from idle. You’ll want to run it from a command line using python .py instead.
    3. Did I mention that you should not use this code aside from testing to say “huh, that did work rather poorly, didn’t it?” If you can find your way around the issue then more power to you but I have to imagine there’s a better way to set this up that sidesteps the issue entirely.

    Good luck with whatever you decide. Don’t use my code. It runs and won’t give you malware but that’s about all it’s got in its favor.

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

Sidebar

Related Questions

I'm trying to write some code which uses pipes to communicate between a parent
I write a custom dialog and try to get some data from its parent
Summary A parent can have many children. How do you write a service such
I need to write some service for my application. I want each client to
I want to have children processes write to the parent's @array. I've read about
I need to write a query like this, any help please? select id, parent,
I write my own class which subclass from QWidget And this is my header
I am trying to understand how to get children to write to a parent's
I have a class Parent which has as property Items that is a List(of
I need to write a program that create pipe send filename from command line

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.