The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Question
00:57 Accepted answer (Score 14)
01:37 Answer 2 (Score 2)
02:05 Thank you

--

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

Accepted answer links:
[Popen.communicate]: https://docs.python.org/2/library/subpro...

--

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)