The printed outputs being broken when doing parallel computation by multiprocessing module in Python

Question:

I suspect this might be a classic problem but I’ve couldn’t find the keyword concerning this issue so I’m here to ask for advice.

Considering the following code regarding multiprocessing module in Python

import multiprocessing

def parallel(n):
    while n <= 10000:
        n += 4
    print(n)
    
with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
    n = [0,1,2,3]
    results = pool.map(parallel, n)

You can test the code at Sage Cell Server

The problem I want to solve is that, I want the printed outputs to be aligned nicely. Such as

10003 

10001

10002

10004

The outputs don’t have to follow any increasing or alphabetical order, I just want those outputs to be aligned throughout the multi-lines.

For the example code I’ve attached above, it works fine in my VSCode.
However, I observe that it gets broken in Sage Cell server

as,

100011000210003 
  
10004

or

1000410001 10003 10002  

If it was just a problem happening in Sage Cell server, I would have just skipped it and use my personal IDE. However, when the code gets bigger and complicated I’ve observed that it also happens in my personal IDE of PyCharm and VSCode.

So, is there any promising method that I can avoid those breakage between the multi-line printed outputs? Looking forward to any kind of advice. Thanks.

[What I’ve tried]

I’ve tried

print(n, 'n')

method. But it didn’t work out.
It seems obvious that another method is required to deal with this problem.

[Edit for the Clarification]

For a clarification of my problem, I would like the outputs to be aligned throughout several lines. Such as,

10004

10001

10002

10003

If outputs are printed as

10004 10001 10002 10003 

then, it is surely better than

100041000110002 10003

However when my desired outputs are not just short integers, but long sentences with some calculated integers inside those sentences, I need them to be printed out throughout multi-lines in alignment.

1. ~~~~~~~10004~~~~~
2. ~~~~~~~10001~~~~~
3. ~~~~~~~10002~~~~~
4. ~~~~~~~10003~~~~~

If they are printed out as

1. ~~~~~~~10004~~~~~ 2. ~~~~~~~10001~~~~~ 3. ~~~~~~~10002~~~~~ 4. ~~~~~~~10003~~~~~

Then, the readability becomes bad. So I need them to be aligned throughout the multi-lines.

Any advice would be grateful. Thanks.

Asked By: user1851281

||

Answers:

you can use format:

print(" {} ".format(n), end="")

you can also use short hand syntax

print(f" {n} ", end="")

for multiline output you return the result from the function parallel

import multiprocessing

def parallel(n):
    while n <= 10000:
        n += 4
    #print(f" {n} ", end="")
    return n
    
with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
    n = [0,1,2,3]
    results = pool.map(parallel, n)
    for i,x in enumerate(results):
        print(f"{i}~~~~{x}~~~~")

output:

0~~~~10004~~~~
1~~~~10001~~~~
2~~~~10002~~~~
3~~~~10003~~~~
Answered By: Hackaholic