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()
Run Code Online (Sandbox Code Playgroud)
此代码将引发异常
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.
Run Code Online (Sandbox Code Playgroud)
但是添加后if __name__ == '__main__': freeze_support()
会引发其他异常,如何解决?我的操作系统是window7
当将多处理与'spawn'启动方法一起使用时(在缺少fork像Windows的平台上为默认设置)并且不使用防护来保护代码时,将显示此错误消息if __name__ = '__main__'。
原因是使用'spawn'start方法产生了一个新的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!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6592 次 |
| 最近记录: |