Issue
I'm trying to make a program that executes a program I have multiple times, that program I am trying to execute repeats indefinitely unless I force close it, which is how i coded it, but that's not the program i need help with, I need help executing that program multiple times heres the code i have right now:
import os
os.system("python3 /Users/sterlingrussell/PycharmProjects/many\ math\ solvers/space\ heater\ exicuter.py")
os.system("python3 /Users/sterlingrussell/PycharmProjects/many\ math\ solvers/space\ heater\ exicuter.py")
os.system("python3 /Users/sterlingrussell/PycharmProjects/many\ math\ solvers/space\ heater\ exicuter.py")
os.system("python3 /Users/sterlingrussell/PycharmProjects/many\ math\ solvers/space\ heater\ exicuter.py")
os.system("python3 /Users/sterlingrussell/PycharmProjects/many\ math\ solvers/space\ heater\ exicuter.py")
The reason the program that I'm executing is called "space heater" is because its designed to make heat. If you want me to give the code for that other program I can. CLARIFICATION: I want this: Image of multiple python processes and not this: image of one python process
Solution
Don't use os.system
, that is deprecated and you should use the subprocess
module. Since you want to open the subprocesses in a non-blocking manner (without waiting for the previous one to terminate) you can use:
import subprocess
import sys
args = [sys.executable, "/path/to/exicuter.py"]
procs = [
subprocess.Popen(args) for _ in range(5)
]
I assume you want to start the program some pre-determined amount of times, in the above case, that is 5 times.
You should read the docs to understand how subprocess.Popen
works if you need any other features (such as retrieving the output). Note, I use sys.executable
to use the same Python as the one that is running this main file to execute the subprocess python scripts.
Answered By - juanpa.arrivillaga
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.