从 python 调用进程的最快方法?
从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我预计 os.system 和 os.execv 以及 subprocess.Popen 之间的任何速度差异都会被以下费用所淹没启动一个新进程(以及实际运行它所需的上下文切换)。因此,我建议首先使用
subprocess
并测量性能。一种可能的性能考虑因素:
os.system
和subprocess.Popen(shell=True, ...)
会导致创建额外的 shell 进程。在大多数情况下,该 shell 是不必要的。创建它是浪费;您获得的进程数量是所需数量的两倍,但没有任何好处。I would expect any speed difference between, say,
os.system
andos.execv
andsubprocess.Popen
, to be swamped by the expense of starting a new process (and the context-switching needed to actually run it). Therefore I recommend usingsubprocess
first and measuring the performance.One possible performance consideration:
os.system
andsubprocess.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.