Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

python - Run Process and Don't Wait

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:WindowsNotepad.exe"
file = "C:PathToFile.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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

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 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...