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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:34:22+00:00 2026-05-27T01:34:22+00:00

I want to add a list of dicts together with python multiprocessing module. Here

  • 0

I want to add a list of dicts together with python multiprocessing module.

Here is a simplified version of my code:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

import multiprocessing
import functools
import time

def merge(lock, d1, d2):
    time.sleep(5) # some time consuming stuffs
    with lock:
        for key in d2.keys():
            if d1.has_key(key):
                d1[key] += d2[key]
            else:
                d1[key] = d2[key]

l = [{ x % 10 : x } for x in range(10000)]
lock = multiprocessing.Lock()
d = multiprocessing.Manager().dict()

partial_merge = functools.partial(merge, d1 = d, lock = lock)

pool_size = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes = pool_size)
pool.map(partial_merge, l)
pool.close()
pool.join()

print d
  1. I get this error when running this script. How shall I resolve this?

    RuntimeError: Lock objects should only be shared between processes through inheritance

  2. is the lock in merge function needed in this condition? or python will take care of it?

  3. I think what’s map supposed to do is to map something from one list to another list, not dump all things in one list to a single object. So is there a more elegant way to do such things?

  • 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-27T01:34:23+00:00Added an answer on May 27, 2026 at 1:34 am

    The following should run cross-platform (i.e. on Windows, too) in both Python 2 and 3. It uses a process pool initializer to set the manager dict as a global in each child process.

    FYI:

    • Using a lock is unnecessary with a manager dict.
    • The number of processes in a Pool defaults to the CPU count.
    • If you’re not interested in the result, you can use apply_async instead of map.
    import multiprocessing
    import time
    
    def merge(d2):
        time.sleep(1) # some time consuming stuffs
        for key in d2.keys():
            if key in d1:
                d1[key] += d2[key]
            else:
                d1[key] = d2[key]
    
    def init(d):
        global d1
        d1 = d
    
    if __name__ == '__main__':
    
        d1 = multiprocessing.Manager().dict()
        pool = multiprocessing.Pool(initializer=init, initargs=(d1, ))
    
        l = [{ x % 5 : x } for x in range(10)]
    
        for item in l:
            pool.apply_async(merge, (item,))
    
        pool.close()
        pool.join()
    
        print(l)
        print(d1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add custom jump list entries to my application in Windows 7.
I want to add colections to List, but only if Advertisements does not already
I have a list of string values that I want add to a hashtable
I want to add a Select One option to a drop down list bound
I have 2 list and i want to add a class =last to the
i have a list box and i want to add a folder/directory to that
I am using the AJAX Cascading drop down list but want to add event
I have an announcements list on one site. I want to add it as
On web page I display list of comments, I want to add an option
What I want to do is add and remove list items. I have got

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.