码迷,mamicode.com
首页 > 系统相关 > 详细

multiprocessing进程开发RuntimeError

时间:2019-01-21 15:52:20      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:not   bootstra   rap   processes   runtime   ted   进程   format   代码   

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

标签:not   bootstra   rap   processes   runtime   ted   进程   format   代码   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!