简体   繁体   English

subprocess.Popen() 和 os.fork() 有什么区别?

[英]What is the difference between subprocess.Popen() and os.fork()?

It seems like subprocess.Popen() and os.fork() both are able to create a child process.似乎subprocess.Popen ()os.fork()都能够创建子进程。 I would however like to know what the difference is between both.但是,我想知道两者之间有什么区别。 When would you use which one?你什么时候用哪一种? I tried looking at their source code but I couldn't find fork()'s source code on my machine and it wasn't totally clear how Popen works on Unix machines.我试着查看他们的源代码,但我在我的机器上找不到 fork() 的源代码,并且不完全清楚 Popen 在 Unix 机器上是如何工作的。

Could someobody please elaborate?有人可以详细说明吗?

Thanks谢谢

subprocess.Popen let's you execute an arbitrary program/command/executable/whatever in its own process. subprocess.Popen让你在它自己的进程中执行任意程序/命令/可执行文件/任何东西。

os.fork only allows you to create a child process that will execute the same script from the exact line in which you called it. os.fork只允许您创建一个子进程,该子进程将从您调用它的确切行执行相同的脚本 As its name suggests, it "simply" forks the current process into 2.顾名思义,它“简单地”将当前进程分叉为 2。

os.fork is only available on Unix, and subprocess.Popen is cross-platfrom. os.fork只能在Unix可用,并且subprocess.Popen是跨洗车台。

So I read the documentation for you.所以我为你阅读了文档。 Results:结果:

  • os.fork only exists on Unix. os.fork只存在于 Unix 上。 It creates a child process (by cloning the existing process), but that's all it does.它创建了一个子进程(通过克隆现有进程),但仅此而已。 When it returns, you have two (mostly) identical processes, both running the same code, both returning from os.fork (but the new process gets 0 from os.fork while the parent process gets the PID of the child process).当它返回时,您有两个(大部分)相同的进程,都运行相同的代码,都从os.fork返回(但新进程从os.fork获取0 ,而父进程获取子进程的 PID)。

  • subprocess.Popen is more portable (in particular, it works on Windows). subprocess.Popen更便携(特别是,它适用于 Windows)。 It creates a child process, but you must specify another program that the child process should execute.它创建一个子进程,但您必须指定子进程应该执行的另一个程序。 On Unix, it is implemented by calling os.fork (to clone the parent process), then os.execvp (to load the program into the new child process).在 Unix 上,它是通过调用os.fork (克隆父进程),然后os.execvp (将程序加载到新的子进程中)来实现的。 Because Popen is all about executing a program, it lets you customize the initial environment of the program.因为Popen是关于执行程序的,所以它可以让您自定义程序的初始环境。 You can redirect its standard handles, specify command line arguments, override environment variables, set its working directory, etc. None of this applies to os.fork .您可以重定向其标准句柄、指定命令行参数、覆盖环境变量、设置其工作目录等。这些都不适用于os.fork

In general, subprocess.Popen is more convenient to use.一般来说, subprocess.Popen使用起来更方便。 If you use os.fork , there's a lot you need to handle manually, and it'll only work on Unix systems.如果你使用os.fork ,你需要手动处理很多事情,而且它只能在 Unix 系统上工作。 On the other hand, if you actually want to clone a process and not execute a new program, os.fork is the way to go.另一方面,如果你真的想克隆一个进程而不是执行一个新程序, os.fork是要走的路。

Subprocess.popen() spawns a new OS level process. Subprocess.popen()产生一个新的操作系统级进程。

os.fork() creates another process which will resume at exactly the same place as this one. os.fork()创建另一个进程,该进程将在与此进程完全相同的位置恢复。 So within the first loop run, you get a fork after which you have two processes, the "original one" (which gets a pid value of the PID of the child process) and the forked one (which gets a pid value of 0).因此,在第一个循环运行中,您会得到一个 fork,之后您有两个进程,“原始进程”(获取子进程 PID 的 pid 值)和分叉的进程(获取 pid 值为 0) .

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

相关问题 Python:同时使用os.fork(),os.pipe()和subprocess.Popen问题 - Python: simultaneous use of os.fork(), os.pipe() and subprocess.Popen issue os.spawnlp和subprocess.Popen有什么区别? - What is the difference between os.spawnlp and subprocess.Popen? Python - os.popen和subprocess.Popen有什么区别? - Python - what is difference in os.popen and subprocess.Popen? subprocess.popen 和 subprocess.run 有什么区别 - What is the difference between subprocess.popen and subprocess.run subprocess.Popen 和 os.system 的区别 - Difference between subprocess.Popen and os.system subprocess.Popen("echo $HOME"... 和 subprocess.Popen(["echo", "$HOME"] 有什么区别? - What's the difference between subprocess.Popen("echo $HOME"... and subprocess.Popen(["echo", "$HOME"] os.fork 和 multiprocessing.Process 之间的行为差​​异 - Difference in behavior between os.fork and multiprocessing.Process subprocess.call()和subprocess.Popen()之间的区别是什么使PIPE对前者不太安全? - What difference between subprocess.call() and subprocess.Popen() makes PIPE less secure for the former? 用subprocess.Popen()替换os.popen() - Replacing os.popen() with subprocess.Popen() 在 subprocess.Popen 中,传递 stdin=None 和 stdin=sys.stdin 有什么区别? - In subprocess.Popen, what's the difference between passing stdin=None and stdin=sys.stdin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM