繁体   English   中英

Python2.7 异常“如果程序可以省略“freeze_support()”行”

[英]Python2.7 Exception "The "freeze_support()" line can be omitted if the program"

import Queue
from multiprocessing.managers import BaseManager

BaseManager.register('get_queue', callable=lambda:  Queue.Queue())

manager = BaseManager(address=('', 5000), authkey='abc')
manager.start()
manager.shutdown()

这段代码会抛出异常

RuntimeError: 
        Attempt to start a new process before the current process
        has finished its bootstrapping phase.

        This probably means that you are on Windows 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 a Windows executable.

但是在我添加if __name__ == '__main__': freeze_support()它会抛出if __name__ == '__main__': freeze_support()异常,如何解决? 我的操作系统是window7

当将多处理与'spawn'启动方法一起使用时(在缺少fork类的平台,如windows)上使用多处理,并且不使用if __name__ = '__main__'保护您的代码时,将显示此错误消息。

原因是使用'spawn'启动方法产生了一个新的python进程,然后又必须导入__main__模块,然后才能继续进行工作。 如果您的程序没有提到的防护措施,则该子进程将尝试再次执行与父进程相同的代码,并生成另一个进程,依此类推,直到您的程序(或计算机)崩溃为止。

该消息不是要告诉您添加freeze_support()行,而是要保护您的程序:

import Queue
from multiprocessing.managers import BaseManager

def main():
    BaseManager.register('get_queue', callable=lambda:  Queue.Queue())

    manager = BaseManager(address=('', 5000), authkey='abc')
    manager.start()
    manager.shutdown()

if __name__ == '__main__':
    # freeze_support() here if program needs to be frozen
    main()  # execute this only when run directly, not when imported!

我这样做但有一个错误

if _name_ == '__main__':main()

NameError: name ' name ' 未定义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM