Can multiprocessing Process class be run from IDLE
Asked Answered
E

1

7

A basic example of multiprocessing Process class runs when executed from file, but not from IDLE. Why is that and can it be done?

from multiprocessing import Process

def f(name):
    print('hello', name)

p = Process(target=f, args=('bob',))
p.start()
p.join()
Ennoble answered 9/2, 2016 at 13:20 Comment(1)
@MathBio I have dupe closed the other with this. The answer on this post is by the dev of Python IDLE and is hence far more superior.Mahler
K
11

Yes. The following works in that function f is run in a separate (third) process.

from multiprocessing import Process

def f(name):
    print('hello', name)

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

However, to see the print output, at least on Windows, one must start IDLE from a console like so.

C:\Users\Terry>python -m idlelib
hello bob

(Use idlelib.idle on 2.x.) The reason is that IDLE runs user code in a separate process. Currently the connection between the IDLE process and the user code process is via a socket. The fork done by multiprocessing does not duplicate or inherit the socket connection. When IDLE is started via an icon or Explorer (in Windows), there is nowhere for the print output to go. When started from a console with python (rather than pythonw), output goes to the console, as above.

Kymry answered 9/2, 2016 at 17:2 Comment(3)
I have previously thought about adding 'Run in Console' to the IDLE Run menu. The previous use cases I had for this, using Windows getch and colorizing print output, are rather esoteric. Seeing print output from multiprocesses is much less so, and is needed for debugging multiprocessing code with printstatements. So I am boosting the priority of this addition.Kymry
IDLE’s standard stream replacements are not inherited by subprocesses created in the execution process, whether directly by user code or by modules such as multiprocessing. If such subprocess use input from sys.stdin or print or write to sys.stdout or sys.stderr, IDLE should be started in a command line window. The secondary subprocess will then be attached to that window for input and output. Source Noticed the fact that you are a core developer for IDLE though, I thought adding a little more official source will be good.Tyrannize
@Tyrannize I could not quote that paragraph in 2016 because I wrote it 2 1/2 years later, and revised it in 2021. It was inspired by questions like this. I will update the doc to include the point in my answer about 'python' versus 'pythonw' on Windows.Kymry

© 2022 - 2024 — McMap. All rights reserved.