multiprocessing进程开发RuntimeError

2019-01-22 02:01:09来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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")

 

 

 


原文链接:https://www.cnblogs.com/exhui/p/10298921.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:我的Python分析成长之路11

下一篇:一分钟搞懂你的博客为什么没人看