gpt4 book ai didi

python - 为什么这个多处理代码会失败?

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:29 26 4
gpt4 key购买 nike

<分区>

def sample():
pass

Process(target=sample).start()
Process(target=sample).start()

上述代码因错误而失败:

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

但是这段代码运行良好:

def sample():
pass
if __name__ == '__main__':
Process(target=sample).start()
Process(target=sample).start()

为什么在这种情况下主模块会影响我的代码执行?我无法正确理解错误消息。

注意:我经历了What does if __name__ == "__main__": do?但无法理解它与我的代码的相关性。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com