我想并行运行四个 .exe。在第一个 .exe 的第一次迭代之后,第二个 .exe 必须启动,而第一个 .exe 继续其第二次迭代,以此类推。目标是四个并行地用数据相互反馈。 exes 是用 Fortran 90 编写的,但代码是在 linux python 中。
import os, threading
e = range(10)
for a in e:
def exe1():
os.system("./exe1")
t1 = threading.Thread(target=exe1, args=())
t1.start()
t1.join()
if a > 0:
for b in e:
def exe2():
os.system("./exe2")
t2 = threading.Thread(target=exe2, args=())
t2.start()
t2.join()
if b > 0:
for c in e
def exe3():
os.system("./exe3")
t3 = threading.Thread(target=exe3, args=())
t3.start()
t3.join()
if c > 0:
for d in e
def exe4():
os.system("./exe4")
t4 = threading.Thread(target=exe4, args=())
t4.start()
t4.join()
这是我的想法,但我没有能力并行运行它们。他们必须每次进行 10 次迭代。
最佳答案
我不会进一步评论定义函数的循环(很奇怪),可能是因为缩进真的关闭了,所以并行的线程可能超过 4 个(我想通了)。
但要回答您的问题,您的 x 可执行文件不会并行运行,只是因为您一启动它就在线程上使用 join()
。
因此主程序在尝试启动另一个线程之前等待当前线程终止。
我会这样做:
thread_list = []
在程序开始时。
每次创建线程时,将其引用存储在thread_list
中:
t1 = Threading.thread(...)
thread_list.append(t1)
然后,删除程序中的所有 join
调用。现在您实际上是在 x 个线程中并行启动 x 个进程。
在程序结束时等待所有线程完成:
for t in thread_list:
t.join()
关于python - 并行运行一个又一个 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42914703/