多线程和多处理是否有或不用此操作:

发布于 2025-01-24 13:15:59 字数 73 浏览 3 评论 0原文

多线程和多处理的工作,或者有或没有此功能, 如果__NAME __ ==“ __ Main __” ??

Will Multithreading and Multiprocessing work with or without this,
if __name__=="__main__" ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2025-01-31 13:15:59

__名称__是一个全局变量,自动初始化以引用字符串 - 模块 其中__ name __ name __出现或特殊值,“ __ main __”,如果出现在脚本或顶级程序中。

此模式在许多python源文件中使用:

if __name__ == "__main__":
    # test or demo the module
    ...

如果将源文件作为脚本运行,则运行测试/演示代码,或者如果源文件为导入

导入 multiprocessing://docs.python.org/3/library/threading.html“ rel =“ nofollow noreferrer”> 螺纹>螺纹> 不会更改if nbsp; __名称 “ __ main __”做,并且if;代码>做。这些模块与任何其他python模块没有什么不同(例如, MATH time sys

__name__ is a global variable that is automatically initialized to refer to a string—either the name of the module in which __name__ appears or, the special value, "__main__", if it appears in a script or top-level program.

This pattern is used in many Python source files:

if __name__ == "__main__":
    # test or demo the module
    ...

The test/demo code is run if you run the source file as a script, or it doesn't get run if the source file is imported as a module.

Importing multiprocessing or threading doesn't change what if __name__ == "__main__" does, and if __name__ == "__main__" doesn't change what multiprocessing or threading do. Those modules are no different in that respect from any other Python module (e.g., math, or time, or sys)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文