Python 运行进程,不要等待

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3516007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:30:22  来源:igfitidea点击:

Run Process and Don't Wait

pythonwindows

提问by Bullines

I'd like to run a process and not wait for it to return. I've tried spawn with P_NOWAIT and subprocess like this:

我想运行一个进程而不是等待它返回。我试过用 P_NOWAIT 和这样的子进程生成:

app = "C:\Windows\Notepad.exe"
file = "C:\Path\To\File.txt"

pid = subprocess.Popen([app, file], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).pid

However, the console window remains until I close Notepad. Is it possible to launch the process and not wait for it to complete?

但是,控制台窗口一直存在,直到我关闭记事本。是否可以启动进程而不等待它完成?

采纳答案by Eike

This call doesn't wait for the child process to terminate (on Linux). Don't ask me what close_fdsdoes; I wrote the code some years ago. (BTW: The documentation of subprocess.Popenis confusing, IMHO.)

此调用不等待子进程终止(在 Linux 上)。不要问我做什么close_fds;几年前我写了代码。(顺便说一句:subprocess.Popen恕我直言,文档令人困惑。)

proc = Popen([cmd_str], shell=True,
             stdin=None, stdout=None, stderr=None, close_fds=True)

Edit:

编辑:

I looked at the the documentation of subprocess, and I believe the important aspect for you is stdin=None, stdout=None, stderr=None,. Otherwise Popencaptures the program's output, and you are expected to look at it. close_fdsmakes the parent process' file handles inaccessible for the child.

我查看了subprocess的文档,我相信对您来说重要的方面是stdin=None, stdout=None, stderr=None,. 否则Popen 会捕获程序的输出,您需要查看它。close_fds使子进程无法访问父进程的文件句柄。

回答by Jon Winn

I finally got this to work. I'm running "Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] win32". Here's how I had to code it:

我终于得到了这个工作。我正在运行“Python 2.6.6 (r266:84297, 2010 年 8 月 24 日,18:13:38) [MSC v.1500 64 位 (AMD64)] win32”。这是我必须对其进行编码的方式:

from subprocess import Popen
DETACHED_PROCESS = 0x00000008
cmd = [
        sys.executable,
        'c:\somepath\someprogram.exe',
        parm1,
        parm2,
        parm3
      ]
p = Popen(cmd,shell=False,stdin=None,stdout=None,stderr=None,close_fds=True,creationflags=DETACHED_PROCESS)

This turns off all piping of standard input/output and does NOT execute the called program in the shell. Setting 'creationflags' to DETACHED_PROCESS seemed to do the trick for me. I forget where I found out about it, but an example is used here.

这会关闭标准输入/输出的所有管道,并且不会在 shell 中执行被调用的程序。将 'creationflags' 设置为 DETACHED_PROCESS 似乎对我有用。我忘记了我在哪里发现的,但这里使用一个例子。

回答by Bruno Oliveira

I think the simples way to implement this is using the os.spawn*family of functions passing the P_NOWAITflag.

我认为实现这一点的最简单方法是使用os.spawn*系列传递P_NOWAIT标志的函数。

This for example will spawn a cpprocess to copy a large file to a new directory and not bother to wait for it.

例如,这将生成一个cp将大文件复制到新目录的进程,而不必费心等待它。

import os
os.spawnlp(os.P_NOWAIT, 'cp', 'cp', '/path/large-file.db', '/path/dest')

回答by idbrii

You are capturing input and output to the program so your program will not terminate as long as it keeps those file descriptors open. If you want to capture, you need to close the file descriptors. If you don't want to capture:

您正在捕获程序的输入和输出,因此只要您的程序保持打开这些文件描述符,它就不会终止。如果要捕获,则需要关闭文件描述符。如果您不想捕获:

app = "C:\Windows\Notepad.exe"
file = "C:\Path\To\File.txt"

pid = subprocess.Popen([app, file]).pid