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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:52:12+00:00 2026-06-17T04:52:12+00:00

My question is regarding the multiprocessing module of Python. In the simplest form, my

  • 0

My question is regarding the multiprocessing module of Python.
In the simplest form, my question is the strange behaviour of the following code:

import numpy as np
from multiprocessing import Pool

x = np.random.random(100)
y = np.random.random(100)
y2 = y[:]

def I(i):
    y[i] = x[i]

pool = Pool()
pool.map(I,range(100))

After the execution, my hope is that y = x.
However, we get y = y2. (The assignments are not working.)
Why is this happening?
What is the best way to compute f(x[i]) and assign it to y[i]?

  • 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-17T04:52:13+00:00Added an answer on June 17, 2026 at 4:52 am

    The behavior you’re seeing is not so surprising if you think about what is being synchronized between the processes used by Pool to do your work. Only the arguments and return values of the I function are synchronized in your current code, so it makes sense that x and y keep their original values in the calling process.

    I suspect your current code is a minimal test case, which is troublesome because there’s not really a meaningful implementation of copying one array to another using Pool.map. Here’s a trivial solution, but I’m not sure it generalizes to whatever your real task is:

    import numpy as np
    from multiprocessing import Pool
    
    def I(v):
        return v
    
    if __name__ == "__main__":  # this boilerplate is required on on Windows
        x = np.random.random(100)
        y = np.random.random(100)
    
        pool = Pool()
        y[:] = pool.map(I, x)
    
        print(x == y) # [True, True, True, ...]
    

    This passes each value of x through to another process (where nothing is done with it) and the result values are passed back and assigned into y (pool.map returns a list). It’s pretty silly.

    A slightly more sophisticated approach might copy x over to the worker processes, using the initializer and initargs arguments in the Pool constructor. Here’s an example that does that:

    import numpy as np
    from multiprocessing import Pool
    
    def I(index):
        return x[index]
    
    def setup(value):
        global x
        x = value
    
    if __name__ == "__main__":
        x = np.random.random(100)
        y = np.random.random(100)
    
        pool = Pool(initializer=setup, initargs=(x,))
        y[:] = pool.map(I, range(100))
    
        print(x == y) # [True, True, True, ...]
    

    Note though that x is only copied one way. If I were to modify its value, the changes would not be synchronized between processes.

    If your task is something that really does requires synchronized access to both the source and target array, you might try out multiprocessing.Array. I don’t have any direct experience with it, but it should be possible to replace y with a synchronized version of itself. Unfortunately, I suspect the synchronization will slow your program down, so don’t do it unless you really need to!

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

Sidebar

Related Questions

One question regarding whether the following code should yield a compiler warning or not
I try to use Pool from the multiprocessing module to speed up reading in
Quick question regarding EventHandlers in C#, let's say we have the following code: MyObject.MyEventHandler
A question regarding everything runs in parallel except your code from someone new to
Quick question regarding memory management in C++ If I do the following operation: pointer
I have a question regarding applying a RTRIM on a ASP:Hyperlink statement. The code
I have a question regarding restricted pointer assignments. See the comments in code for
Following my previous question regarding the rationale behind extremely long functions, I would like
I have a question regarding the std::sort algorithm. Here is my test code: struct
I have a question regarding importing( Via the import/export wizard) an excel spreadsheet into

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.