从 python 调用进程的最快方法?

发布于 2024-10-08 19:40:13 字数 109 浏览 0 评论 0原文

从 python 执行可执行文件的最快/最有效的方法是什么?在我看来 os.system 比 subprocess.popen 更快。我希望能够阅读其他过程中产生的代码,但比其他任何事情都更重要的是速度。

What is the fastest/most efficient method to execute an executable from python? It seems to me that os.system is faster than subprocess.popen. I would like to be able to read the lines that come out of this other process, but far more important than anything else is speed.

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

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

发布评论

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

评论(1

ゃ懵逼小萝莉 2024-10-15 19:40:13

我预计 os.system 和 os.execv 以及 subprocess.Popen 之间的任何速度差异都会被以下费用所淹没启动一个新进程(以及实际运行它所需的上下文切换)。因此,我建议首先使用 subprocess 并测量性能。

一种可能的性能考虑因素:os.systemsubprocess.Popen(shell=True, ...) 会导致创建额外的 shell 进程。在大多数情况下,该 shell 是不必要的。创建它是浪费;您获得的进程数量是所需数量的两倍,但没有任何好处。

I would expect any speed difference between, say, os.system and os.execv and subprocess.Popen, to be swamped by the expense of starting a new process (and the context-switching needed to actually run it). Therefore I recommend using subprocess first and measuring the performance.

One possible performance consideration: os.system and subprocess.Popen(shell=True, ...) cause an extra shell process to be created. In most cases, that shell isn't necessary. It's wasteful to create it; you get twice as many processes as you need for no benefit.

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