本文介绍了运行进程,不要等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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?

推荐答案

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

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

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

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

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

这篇关于运行进程,不要等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 19:25