Running executables concurrently in Python

Question:

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())
Asked By: user19977266

||

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()
Answered By: tdelaney
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.