How to detach a program ran by subprocess.call?

2019-08-30 01:06发布

问题:

I am opening a pdf file with my default application using subprocess.call, like this:

subprocess.call(["xdg-open", pdf], stderr=STDOUT)

But, when running that, the process is attached to the terminal and I want to detach it. Basically, I want to run that and then be able to use the terminal for other stuff.

How would I go about doing that?

回答1:

You can use Popen for this.

from subprocess import Popen, PIPE, STDOUT
p = Popen(["xdg-open", pdf], stderr=STDOUT, stdout=PIPE)
# do your own thing while xdg-open runs as a child process
output, _ = p.communicate()