简体   繁体   English

如何导入需要 __name__ == "__main__" 的脚本

[英]How to import script that requires __name__ == "__main__"

I'm pretty new to Python, this question probably shows that.我对 Python 很陌生,这个问题可能表明了这一点。 I'm working on multiprocessing part of my script, couldn't find a definitive answer to my problem.我正在处理我的脚本的多处理部分,找不到我的问题的明确答案。

I'm struggling with one thing.我正在为一件事而苦苦挣扎。 When using multiprocessing, part of the code has to be guarded with if __name__ == "__main__" .使用多处理时,必须使用 if __name__ == "__main__"保护部分代码。 I get that, my pool is working great.我明白了,我的游泳池工作得很好。 But I would love to import that whole script (making it a one big function that returns an argument would be the best).但我很想导入整个脚本(让它成为一个返回参数的大 function 是最好的)。 And here is the problem.这就是问题所在。 First, how can I import something if part of it will only run when launched from the main/source file because of that guard?首先,如果由于那个守卫,它的一部分只能在从主/源文件启动时运行,我该如何导入? Secondly, if I manage to work it out and the whole script will be in one big function, pickle can't handle that, will use of "multiprocessing on dill" or "pathos" fix it?其次,如果我设法解决它并且整个脚本将在一个大 function 中,pickle 无法处理,将使用“多处理莳萝”或“pathos”修复它吗?

Thanks!谢谢!

You are probably confused with the concept.您可能对这个概念感到困惑。 The if __name__ == "__main__" guard in Python exists exactly in order for it to be possible for all Python files to be importable. Python 中的if __name__ == "__main__"保护正是为了让所有 Python 文件都可以导入而存在。

Without the guard, a file, once imported, would have the same behavior as if it were the "root" program - and it would require a lot of boyler plate and inter-process comunication (like writting a "PID" file at a fixed filesystem location) to coordinate imports of the same code, including for multiprocessing.如果没有警卫,文件一旦导入,将具有与“根”程序相同的行为- 并且需要大量的 boiler 板和进程间通信(例如在固定位置写入“PID”文件)文件系统位置)来协调相同代码的导入,包括多处理。

Just leave under the guard whatever code needs to run for the root process.只需将需要为根进程运行的任何代码置于保护之下。 Everything else you move into functions that you can call from the importing code.您移动到可以从导入代码调用的函数中的所有其他内容。

If you'd run "all" the script, even the part setting up the multiprocessing workers would run, and any simple job would create more workers exponentially until all machine resources were taken (ie: it would crash hard and fast, potentially taking the machine to an unresponsive state).如果您运行“所有”脚本,即使设置多处理工作人员的部分也会运行,并且任何简单的工作都会以指数方式创建更多工作人员,直到所有机器资源都被占用(即:它会快速崩溃,可能会占用机进入无响应状态)。

So, this is a good pattern - th "dothejob" function can call all other functions you need, so you just need to import and call it, either from a master process, or from any other project importing your file as a Python module.所以,这是一个很好的模式 - “dothejob” function 可以调用您需要的所有其他功能,因此您只需从主进程或任何其他项目导入并调用它,将您的文件作为 Python 模块导入。


import multiprocessing
...
def dothejob():
   ...

def start():
   # code to setup and start multiprocessing workers:
   # like:
   worker1 = multiprocessing.Process(target=dothejob)
   ...
   worker1.start()
   ...
   worker1.join()

if __name__ == "__main__":
   start()

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

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