python - 并行运行一个又一个 exe

标签 python linux parallel-processing

我想并行运行四个 .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/

相关文章:

python - 新闻聚类程序不显示Python中的链接

python - 查看列表中的值是否相等 ~Python

c - 上游维护者的工具?用于发布前的测试(Debian等)

java - 用于列出源文件中所有函数的 linux 工具?

python-3.x - Python3.8 无法安装 shap、llvmlite 或 numba

scala - 在scala中的另一个线程上执行一个简单的任务

r - 有没有另一种方法可以在 worker 中加载额外的包(并行计算)?

python - 从 Go 中的 Python 项目加载数据存储实体会导致嵌套结构 slice 错误

python - 在 Windows 中为 Python 安装 thrift_sasl

Python - 使用 joblib 进行循环并行化