""" The test shows race condition when executed Python script is removed right after the Python started, so that Python is unable to read it. """ import os import sys import tempfile print(__doc__) longscript = """\ import time for x in range(5): print(x) time.sleep(1) """ (fd, abspath) = tempfile.mkstemp('.subprocess.py') os.write(fd, longscript) os.close(fd) python = sys.executable cmd = [python, abspath] from subprocess import Popen, PIPE # this returns immediately leaving child process # to be executed in background p = Popen(cmd) os.remove(abspath)