I can't seem to get multiprocessing to work on my laptop

Viewed 74

this is my first time posting on stackoverflow, so I hope I do not mess it up.

I am a beginner in Python, and I wanted to attempt Multi-processing. But I've spent two days following basic tutorials about it, and none of them seem to work the way I want it to. An example of what I'm trying:

import os

from multiprocessing import Process, current_process

def square(number):
    result = number * number

    proc_id = os.getpid()
    print(f"Process ID: {proc_id}")

    process_name = current_process().name
    print(f"Process Name: {process_name}")

    print(f"The number {number} squares to {result}.")


if __name__ == "__main__":
    processes = []
    numbers = [1, 2, 3, 4, 5]

    for i, number in enumerate(numbers):
        process = Process(target=square, args=(number,))
        processes.append(process)

        process.start()

    for process in processes:
        process.join()

A simple code, but all that comes out on the compiler is blank.

Yet if I change this line here:

process = Process(target=square, args=(number,))

To:

process = Process(target=square(number))

It works now! But the output would then be wrong.

My ideal output

    Process ID: 10000
    
    Process Name: Process - 1
    
    The number 1 squares to 1.
    
    
    
    Process ID: 10001
    
    Process Name: Process - 2
    
    The number 2 squares to 4.
    
    
    
    Process ID: 10002
    
    Process Name: Process - 3
    
    The number 3 squares to 9.
    
    
    
    Process ID: 10003
    
    Process Name: Process - 4
    
    The number 4 squares to 16.

Actual output

Process ID: 10000

Process Name: MainProcess

The number 1 squares to 1.



Process ID: 10000

Process Name: MainProcess

The number 2 squares to 4.



Process ID: 10000

Process Name: MainProcess

The number 3 squares to 9.



Process ID: 10000

Process Name: MainProcess

The number 4 squares to 16.

I'm using Windows 10, Visual Studio 2019, Python 3.7.

I'm not sure if the solution is simple, but my brain is square and I need help.

2 Answers

When you create a multiprocessing.Process the subprocess inherits the parent's stdout and can write to it. The problem is, that stdout may not being going anywhere you can read it. When you run from a GUI like an IDE, stdout is usually redirected to oblivion. An IDE instruments the running process to grab its writes to sys.stdout and redirect to is command output window, but that doesn't flow down to the subprocess.

Unfortunately there isn't an easy way to grab the stdout stream in a multiprocessin.Process. Its easy when you just execute a different program through something like subprocess.

The good news is that its rare for a subprocess to want to write to stdout in the first place. multiprocessing has several options such as Queue and Pipe to get things back to the parent process.

It's not quite the question you asked, but I strongly recommend you use multiprocessing.Pool whenever possible. It saves you a lot of headache and bookkeeping. Your entire __main__ would be

from multiprocessing import Pool

if __name__ == "__main__":
    numbers = [1, 2, 3, 4, 5]
    with Pool() as pool:
        pool.map(square, numbers)
Related