多处理中的输出相关错误

Output related error in multiprocessing

这是我的多处理代码。

from multiprocessing import Process

def print_func(continent = 'Asia'):
    print('The name of continent is: ',continent)

if __name__ == "__main__":
    names = ['America','Russia','Africa']
    procs = []
    proc = Process(target = print_func)
    procs.append(proc)
    proc.start()

    for name in names:
        proc = Process(target = print_func,args =(name,))
        procs.append(proc)
        proc.start()

    for proc in procs:
        proc.join()

但是没有输出!我做错了什么?

我想要这样的输出:

The name of continent is: Asia
The name of continent is: America
The name of continent is: Russia
The name of continent is: Africa

我的 Whosebug 感觉告诉我你将其粘贴到 python shell。您需要将其放入 python 文件 ,例如continents.py ,然后像这样执行解释器

python continents.py

或者,您可以删除 if __name__ == "__main__":,当粘贴到 python shell.

时,它应该也可以直接工作

查看实际效果 here

and this bug report所述,这是IDLE的设计限制。由于 IDLE 仅连接到已启动的进程而不是其子进程,因此它不会捕获输出。

运行另一个PythonIDE中的代码(IDLE主要针对初学者),或者只是运行没有IDE的程序,即(假设您的程序文件名为 multiprint.py)和

python multiprint.py