Reputation: 1653
I want to run two or more python scripts simultaneously from a master script
. Each of these scripts already have threads within them which are running in parallel. For example I run
script1.py
if __name__ == '__main__':
pid_vav = PID_VAV('B2')
t1 = threading.Thread(target=pid_vav.Controls)
t1.daemon = False
t1.start()
t2 = threading.Thread(target=pid_vav.mqttConnection)
t2.daemon = False
t2.start()
script2.py
if __name__ == '__main__':
pid_vav = PID_VAV('B4')
t1 = threading.Thread(target=pid_vav.Controls)
t1.daemon = False
t1.start()
t2 = threading.Thread(target=pid_vav.mqttConnection)
t2.daemon = False
t2.start()
I am running this script1.py
and script2.py
separately. Only difference is the parameter which I am passing to the class. Is it possible to have a master script such that if I just run that, both these scripts will run ?
Thanks
Upvotes: 4
Views: 5002
Reputation: 91
If you're happy to have the code for both live in one file, you can use multiprocessing to run them concurrently on different CPU cores.
import multiprocessing as mp
from threading import Thread
def start_process(pid_vav_label):
pid_vav, threads = PID_VAV(pid_vav_label), []
threads.append(Thread(target=pid_vav.Controls))
threads.append(Thread(target=pid_vav.mqttConnection))
for thread in threads:
thread.start()
# Join if necessary
for thread in threads:
thread.join()
if __name__ == '__main__':
processes = []
for label in ['B2', 'B4']:
processes.append(mp.Process(target=start_process, args=(label,)))
processes[-1].start()
# Again, can join if necessary
for process in processes:
process.join()
Upvotes: 0
Reputation: 920
Assuming you want the output of both scripts to be shown when you run the master script.
You can make use of the subprocess
module to call the python file, and you can use the threading
module to start separate threads
from threading import Thread
import subprocess
t1 = Thread(target=subprocess.run, args=(["python", "script1.py"],))
t2 = Thread(target=subprocess.run, args=(["python", "script2.py"],))
t1.start()
t2.start()
t1.join()
t2.join()
Upvotes: 5
Reputation: 407
Yes, ofc.
script_master.py:
from os import system
system('start script1.py && start script2.py')
But I think you could to use this code:
script_together.py:
if __name__ == '__main__':
todo=[]
todo.append(threading.Thread(target=lambda: PID_VAV('B2').Controls, daemon=False))
todo.append(threading.Thread(target=lambda: PID_VAV('B4').mqttConnection, daemon=False))
for th in todo:
th.start()
for th in todo:
th.join()
Upvotes: 1
Reputation: 111
If u want to trigger 2 scripts from a master script u can use the below method. It will help you trigger both scripts as thread and the thread can also produce different threads based on the callable scripts. You can even make Scripts run independently.
import subprocess
pid1 = subprocess.Popen([sys.executable, "script1.py"])
pid2 = subprocess.Popen([sys.executable, "script2.py"])
Upvotes: 2