Search code examples
pythonpython-3.xwindowssubprocess

Execute independent Python file that continues running even after parent file terminates on Windows


I have been scouring the Internet for a solutions and while there have been similar threads to this, they appear to be a little dated or for *nix systems. Here is my situation:

I have a Python 3.9 process running that does a bunch of checks and towards the end of it, will send an email to me regarding them. Sometimes this email process fails and instead of waiting and continuing to retry sending the email before proceeding, I want to pass it off to a completely independent process that will continue trying to send the email even if the parent/main process terminates early.

Here is some mock code of what I have been trying: main process.py

import subprocess
import time

#doing checks and what not
#email fails to send at first
p1 = subprocess.Popen(['python','path/to/test_email_send.py', 'test1', 'test2'], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP|subprocess.DETACHED_PROCESS, start_new_session=True)

time.sleep(5)
#continue doing some other stuff but ultimately finishing before the email retry can finish

I have tried this as well as multiple variations (multiprocessing with Daemon, different creationflags, executing a .bat file which does a pythonw of the test_email_send.py) of the above code. Every time, the above subprocess starts but the minute it gets past the time.sleep(5) and the parent/main script finishes/terminates, the subprocess terminates. I don't need to check on the subprocess at the end of the parent script, I just want it to detach from the parent and continue retrying to send the email (say for 30 minutes or until successful) in the background.

Help pls.


Solution

  • I looked around and found the following solution. I dont know if its good practice or if it fits your needs.

    For the main.py I have:

    import subprocess
    import time
    
    p = subprocess.Popen("python test.py", start_new_session=True)
    
    time.sleep(5)
    print("finished main.py")
    

    And for the test.py:

    import time
    
    print("test.py started")
    
    for _ in range(10):
        time.sleep(1) 
        print("still working")  
    
    print("test.py finished")