原始代码如下:
from multiprocessing import Process
import time
def test():while True:print("------子进程---------")time.sleep(3)p = Process(target=test)
p.start()
while True:print("------------main------------")time.sleep(3)
报错为:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start yourchild processes and you have forgotten to use the proper idiomin the main module:if __name__ == '__main__':freeze_support()...根据提示信息说的是, 你既没有用fork (linux 和mac 支持),也没有 用proper idiom ,所以要报错,
它很智能给你了解决方法, 加上 if __name__ == ‘__main__’:
修改后 就不报错了 如下;
from multiprocessing import Process
import time
def test():while True:print("------子进程---------")time.sleep(3)
def main():while True:print("------------main------------")time.sleep(3)if __name__ == '__main__':p = Process(target=test)p.start()main()