Python multiprocessing child process print in Anaconda prompt but not in jupyter notebook

Viewed 16

System: Windows 10 64 bits and Jupyter Notebook with Python 3.9.12.

I have a .py file with the following codes:

from time import sleep

def task():
    print('Hello from child process')
    sleep(1)

and an .ipynb:

from tasks import task
import torch.multiprocessing as mp
if __name__ == '__main__':
    print('Hello from main process')
    process = mp.Process(target=task)
    process.start()
    process.join()
    print('Hello again from main process')

While jupyter notebook printed the following 2 lines,

Hello from main process
Hello again from main process

Anaconda prompt (the black screen) return

Hello from child process

Is it possible to move the 'Hello from child process' line to the jupyter notebook?

That is

Hello from main process
Hello from child process
Hello again from main process
1 Answers

A possible workaround solution is to write the print message into a txt file, and read it in the main file.

Print function or even error message of child process only appears in terminal.

.py file

from time import sleep

def task():
    with open('test.txt', 'w') as f:
        f.write('Hello from child process')
    sleep(1)

an .ipynb:

import torch.multiprocessing as mp
from tasks import task

if __name__ == '__main__':
    print('Hello from main process')
    process = mp.Process(target=task)
    process.start()
    process.join()
    
    with open('test.txt', 'r') as f:
        print(f.read())
    
    print('Hello again from main process')
Related