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

  • Home
  • SEARCH
  • 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 8761033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:11:35+00:00 2026-06-13T15:11:35+00:00

I wanted to try different ways of using multiprocessing starting with this example: $

  • 0

I wanted to try different ways of using multiprocessing starting with this example:

$ cat multi_bad.py 
import multiprocessing as mp
from time import sleep
from random import randint

def f(l, t):
#   sleep(30)
    return sum(x < t for x in l)

if __name__ == '__main__':
    l = [randint(1, 1000) for _ in range(25000)]
    t = [randint(1, 1000) for _ in range(4)]
#   sleep(15)
    pool = mp.Pool(processes=4)
    result = pool.starmap_async(f, [(l, x) for x in t])
    print(result.get())

Here, l is a list that gets copied 4 times when 4 processes are spawned. To avoid that, the documentation page offers using queues, shared arrays or proxy objects created using multiprocessing.Manager. For the last one, I changed the definition of l:

$ diff multi_bad.py multi_good.py 
10c10,11
<     l = [randint(1, 1000) for _ in range(25000)]
---
>     man = mp.Manager()
>     l = man.list([randint(1, 1000) for _ in range(25000)])

The results still look correct, but the execution time has increased so dramatically that I think I’m doing something wrong:

$ time python multi_bad.py 
[17867, 11103, 2021, 17918]

real    0m0.247s
user    0m0.183s
sys 0m0.010s

$ time python multi_good.py 
[3609, 20277, 7799, 24262]

real    0m15.108s
user    0m28.092s
sys 0m6.320s

The docs do say that this way is slower than shared arrays, but this just feels wrong. I’m also not sure how I can profile this to get more information on what’s going on. Am I missing something?

P.S. With shared arrays I get times below 0.25s.

P.P.S. This is on Linux and Python 3.3.

  • 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-13T15:11:38+00:00Added an answer on June 13, 2026 at 3:11 pm

    Linux uses copy-on-write when subprocesses are os.forked. To demonstrate:

    import multiprocessing as mp
    import numpy as np
    import logging
    import os
    
    logger = mp.log_to_stderr(logging.WARNING)
    
    def free_memory():
        total = 0
        with open('/proc/meminfo', 'r') as f:
            for line in f:
                line = line.strip()
                if any(line.startswith(field) for field in ('MemFree', 'Buffers', 'Cached')):
                    field, amount, unit = line.split()
                    amount = int(amount)
                    if unit != 'kB':
                        raise ValueError(
                            'Unknown unit {u!r} in /proc/meminfo'.format(u = unit))
                    total += amount
        return total
    
    def worker(i):
        x = data[i,:].sum()    # Exercise access to data
        logger.warn('Free memory: {m}'.format(m = free_memory()))
    
    def main():
        procs = [mp.Process(target = worker, args = (i, )) for i in range(4)]
        for proc in procs:
            proc.start()
        for proc in procs:
            proc.join()
    
    logger.warn('Initial free: {m}'.format(m = free_memory()))
    N = 15000
    data = np.ones((N,N))
    logger.warn('After allocating data: {m}'.format(m = free_memory()))
    
    if __name__ == '__main__':
        main()
    

    which yielded

    [WARNING/MainProcess] Initial free: 2522340
    [WARNING/MainProcess] After allocating data: 763248
    [WARNING/Process-1] Free memory: 760852
    [WARNING/Process-2] Free memory: 757652
    [WARNING/Process-3] Free memory: 757264
    [WARNING/Process-4] Free memory: 756760
    

    This shows that initially there was roughly 2.5GB of free memory.
    After allocating a 15000×15000 array of float64s, there was 763248 KB free. This roughly makes sense since 15000**2*8 bytes = 1.8GB and the drop in memory, 2.5GB – 0.763248GB is also roughly 1.8GB.

    Now after each process is spawned, the free memory is again reported to be ~750MB. There is no significant decrease in free memory, so I conclude the system must be using copy-on-write.

    Conclusion: If you do not need to modify the data, defining it at the global level of the __main__ module is a convenient and (at least on Linux) memory-friendly way to share it among subprocesses.

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

Sidebar

Related Questions

I have tried a bunch of different ways to do this but every time
I wanted to try to allocate a 4 billion bytes array and this is
I have wanted to try GAE since launch, but coming from ASP .NET and
A simple question. Does this version support generators? I wanted to test out using
Here is the latest code. I wanted to try something different but I am
I wanted to try a program distributed in source for *nux on Windows. It's
so I wanted to try my first CLR project in Visual C++. So I
I just discovered forge and wanted to try it out at once. I closely
I just started out with Python and wanted to try out tornado. Running the
I've read about Single-Page Applications recently and wanted to try one out. After watching

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.