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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:31:20+00:00 2026-05-21T07:31:20+00:00

The piece of code that I have looks some what like this: glbl_array =

  • 0

The piece of code that I have looks some what like this:

glbl_array = # a 3 Gb array

def my_func( args, def_param = glbl_array):
    #do stuff on args and def_param

if __name__ == '__main__':
  pool = Pool(processes=4)
  pool.map(my_func, range(1000))

Is there a way to make sure (or encourage) that the different processes does not get a copy of glbl_array but shares it. If there is no way to stop the copy I will go with a memmapped array, but my access patterns are not very regular, so I expect memmapped arrays to be slower. The above seemed like the first thing to try. This is on Linux. I just wanted some advice from Stackoverflow and do not want to annoy the sysadmin. Do you think it will help if the the second parameter is a genuine immutable object like glbl_array.tostring().

  • 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-21T07:31:21+00:00Added an answer on May 21, 2026 at 7:31 am

    You can use the shared memory stuff from multiprocessing together with Numpy fairly easily:

    import multiprocessing
    import ctypes
    import numpy as np
    
    shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)
    shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
    shared_array = shared_array.reshape(10, 10)
    
    #-- edited 2015-05-01: the assert check below checks the wrong thing
    #   with recent versions of Numpy/multiprocessing. That no copy is made
    #   is indicated by the fact that the program prints the output shown below.
    ## No copy was made
    ##assert shared_array.base.base is shared_array_base.get_obj()
    
    # Parallel processing
    def my_func(i, def_param=shared_array):
        shared_array[i,:] = i
    
    if __name__ == '__main__':
        pool = multiprocessing.Pool(processes=4)
        pool.map(my_func, range(10))
    
        print shared_array

    which prints

    [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 2.  2.  2.  2.  2.  2.  2.  2.  2.  2.]
     [ 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.]
     [ 4.  4.  4.  4.  4.  4.  4.  4.  4.  4.]
     [ 5.  5.  5.  5.  5.  5.  5.  5.  5.  5.]
     [ 6.  6.  6.  6.  6.  6.  6.  6.  6.  6.]
     [ 7.  7.  7.  7.  7.  7.  7.  7.  7.  7.]
     [ 8.  8.  8.  8.  8.  8.  8.  8.  8.  8.]
     [ 9.  9.  9.  9.  9.  9.  9.  9.  9.  9.]]

    However, Linux has copy-on-write semantics on fork(), so even without using multiprocessing.Array, the data will not be copied unless it is written to.

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

Sidebar

Related Questions

I have a piece of C code that looks like this: const char (*foo)[2]
I have some code in my application that looks something like this: char *hash
There's a piece of code that looks like this. The problem is that during
I have a piece of code here that i really could use some help
I have a piece of C# code that needs to convert a string array
I have something that looks like this: As you can see, Blambo is a
I have a piece of code that works in a background process which looks
I have a piece of C# code that add the values of an enum
Let's say I have to implement a piece of T-SQL code that must return
I have some code (which I cannot change) that I need to get working

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.