The Python Oracle

How to run Python's subprocess and leave it in background

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Book End

--

Chapters
00:00 How To Run Python'S Subprocess And Leave It In Background
00:42 Accepted Answer Score 14
01:15 Answer 2 Score 3
01:35 Thank you

--

Full question
https://stackoverflow.com/questions/3471...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #subprocess #popen

#avk47



ACCEPTED ANSWER

Score 14


From the Popen.communicate documentation (emphasis mine):

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.

If you don't want to wait for the process to terminate, then simply don't call communicate:

subprocess.Popen(full_command, close_fds=True)



ANSWER 2

Score 3


Might be another way of doing it, with the optional capability to inspect the subprocess output for a while, from github.com/hologram-io/hologram-python pppd.py file:

    self.proc = Popen(self._commands, stdout=PIPE, stderr=STDOUT)

    # set stdout to non-blocking
    fd = self.proc.stdout.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)