How to prevent subprocess.run() (python 3.11) from waiting to close Edge browser to go next command

316 views Asked by At

Previously, subprocess.run() still only opened the Edge browser and executed the next command without closing Edge. Currently, subprocess.run() will wait for Edge to close before executing the next statement. How do I make run() work like before? I use Python 3.11 and did not make any new settings.

My example code:

edge = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
subprocess.run(edge.split())
print('next command')

run() will wait until I close Edge; when I close Edge, print will execute; but previously run() would not require me to close Edge.

Additionally, I encounter this problem on Firefox, Chrome, and even when I use os.system

1

There are 1 answers

3
tripleee On

subprocess.run() (and indeed also os.system()) always waits for the subprocess to finish, and always has. Something probably changed in Edge, or you are running it differently.

(Vaguely, I recall that running a new browser instance would block, whereas if it was already running, the subprocess would simply launch a new browser window and exit. But I don't remember which browser or which OS I used at the time. I don't use Windows and have never tried Edge. My impression is that Windows processes generally behave somewhat haphazardly when it comes to whether you should expect a new process to just open a window and quit, or stick around until whatever you started has really stopped running. Maybe also experiment with wrapping the process in cmd /c "start /wait" vs not.)

If you want a background process, you need subprocess.Popen(), with all the additional mundane responsibilities to manage (in particular, at the very least wait for and reap) the subprocesses you create.