带有 print() 的 Python 多处理不起作用

问题描述

我正在学习使用 python 进行多处理,但它似乎不适用于 print() 函数和 IDLE 的 shell。

此外,将进程作为守护进程似乎也不起作用,因为当主程序结束时该进程不会被杀死。

这是我写的代码,我希望有人能解释一下可能出错的地方:

import multiprocessing
import time


def proc1(x):                                    # Creates a function to be used inside a process
    for i in range(x):
        print("proc1 is running")       # It seems like the Child Processes doesn't print the "print()" function into the IDLE's shell

if __name__ == '__main__':              # Important!!! we dont want to create endless subprocesses by mistake
    
    proc = multiprocessing.Process(name='proc of Python',target=proc1,args=[300000])
    proc.daemon = True                  # make the process a Daemon and get killed with the end of the Main Program   - doesn't seems to work in this example,I can see the process keep running on Task Manager
    proc2 = multiprocessing.Process(name='proc2 of Python',args=[300000])
    proc2.start()  
    proc.start()
 #   proc.join()
    print('Multi Processing is hard!!\n\n')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)