0

I have searched and tried a lot of codes for that topic. I am trying to run two python files but to run both at the same time This is my try

import subprocess subprocess.run("py pop1.py & py pop2.py", shell=True) 

But this executes the first python then the second one. This is not the target. My target is to run both files at the same time.

3
  • What shell are you invoking? In Bash, the & puts the thing on the left into a background task, so the two actually do run simultaneously. But mileage may vary on other shells Commented Apr 16, 2022 at 23:05
  • Based on the use of the py command, I'm guessing you are on Windows. The Windows command line does not support starting multiple commands at once. They'll run serially. You can simulate that by using multiple calls to subprocess. Commented Apr 16, 2022 at 23:20
  • Yes, I am using Windows. Is there any workaround to run the file at the same time? Commented Apr 16, 2022 at 23:44

1 Answer 1

1

subprocess can do this all on its own without invoking shell=True with the & bashism.

import subprocess # start processes running in parallel p1 = subprocess.Popen(['py', 'pop1.py']) p2 = subprocess.Popen(['py', 'pop2.py']) # wait for both processes to complete p1.wait() p2.wait() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.