首页 > 系统服务 > 详细

multiprocessing进程开发RuntimeError

时间:2019-01-21 15:35:18      阅读:478      评论:0      收藏:0      [点我收藏+]

windows环境下multiprocessing报如下异常信息:

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 your
child processes and you have forgotten to use the proper idiom
in the main module:

if __name__ == ‘__main__‘:
freeze_support()
...

The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.

解决办法:

在启进程的代码前面添加 if __name__ == "__main__":

代码如下:

import multiprocessing as mp
import os
import time

def th1():
    print("this is th1,the time is {0}".format(time.ctime()))
    #getppid获取父进程ID, getpid获取当前进程ID
    print("th1",os.getppid(), "---", os.getpid())

def setproc():
    p1 = mp.Process(target = th1)
    p1.start()
    print("main", os.getppid(), "---", os.getpid())
    
if __name__ == "__main__":
    #p1 = mp.Process(target = th1)
    #p1.start()

    setproc()
    print("main end")

 

 

 

multiprocessing进程开发RuntimeError

原文:https://www.cnblogs.com/exhui/p/10298921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!