Closing a shell after a subprocess command

Hello everyone, it’s my first post !
I’ve got a problem when i want to create a python file to launch a server and end it with a condition…

Actualy, i have to launch a server in local and i open a window in edge with the good path.
i let the terminal open in background to keep the acces to my server, but i want to close it when i’m closing my edge window.

Here is a part of my code :

 command1 = [os.path.join(chemin_certification, "virtuel", "Scripts", "activate")]
  
    chemin_manage_py = os.path.join(chemin_certification, "manage.py")
  
    command2 = [sys.executable, chemin_manage_py, "runserver", "8000"]
    
    subprocess.Popen(command1, shell=True, stdout=subprocess.PIPE)
    subprocess.Popen(command2, cwd=chemin_certification)
    print("test")

    time.sleep(5)
    
    while True:
        if not any("msedge.exe" in p.name() for p in psutil.process_iter()):
            sys.exit()
        time.sleep(1)

Everything that happens after the two subprocess.Popen command seems to not work…(“test” does not appear in my shell). So the server is launch and i can use it in my browser, but my shell’s still open when i close it.

I’ve tried to use async fnction, but the result seems to be the same.

If somebody can help me, it will be a pleasure!

Sorry if my english is bad, i’m french guy.

Thanks !

Are you activating an venv in command1?

It is not necessary to active a venv to be able to use it.
You can use the full path to the python.exe inside the venv and everything will work correctly.

In place of sys.executable use the python.exe from the venv.