简体   繁体   中英

Running executables concurrently in Python

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())

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM