【问题标题】:Run an exe after another in parallel一个接一个地并行运行一个exe
【发布时间】:2017-03-20 21:56:28
【问题描述】:

我想并行运行四个 .exe。在第一个 .exe 的第一次迭代之后,第二个 .exe 必须启动,而第一个继续其第二次迭代,以此类推。目标是四个并行的数据相互反馈。 exe是用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 次迭代。

【问题讨论】:

  • 如果你 join 只要你 start 你的线程,难怪它为什么不并行运行。
  • 那里只有一个函数定义的循环是什么?病了!
  • 看看subprocess 模块。它可能更适合您正在做的事情。
  • 我不认为并行意味着你认为的意思,我的朋友。
  • 你的缩进真的不对。差点让我错过了重点。

标签: python linux parallel-processing


【解决方案1】:

我不会进一步评论定义函数的循环(很奇怪),可能是因为缩进真的关闭了,所以可能有超过 4 个并行线程(我想出了这么多)。

但要回答您的问题,您的 x 可执行文件不会并行运行,因为您一开始就在线程上使用join()

所以主程序在尝试启动另一个线程之前等待当前线程终止。

我会这样做:

thread_list = []

在程序开始时。

每次创建线程时,将其引用存储在thread_list

t1 = Threading.thread(...)
thread_list.append(t1)

然后,删除程序中的所有 join 调用。现在,您实际上是在 x 个线程中并行启动 x 个进程。

并在程序结束时等待所有线程完成:

for t in thread_list:
    t.join()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多