Running executables concurrently in Python

Viewed 21

I have 3 folders, namely, 1, 2, 3 each with the following executable. How can I run all of them concurrently at once?

exec(open("Test.py").read())
1 Answers

Since you want to run these .py files as separate processes, you should be able to execute them via the same python executable your main script is using. Use Popen to start all of the processes and then wait on them in turn.

#!/usr/bin/env python3

import os
import sys
import subprocess as subp

folder_names = ["1", "2", "3"]
procs = [subp.Popen([sys.executable, os.path.join(folder, "Test.py"])
    for folder in folder_names]
for proc in procs:
    return_code = proc.wait()
Related