【问题标题】:how to send a bash command to command line using python and execute next command without waiting for bash command to execute? [duplicate]如何使用 python 将 bash 命令发送到命令行并执行下一个命令而不等待 bash 命令执行? [复制]
【发布时间】:2019-05-04 04:48:01
【问题描述】:

我想在超级计算机节点的 3 个独立核心处理器上执行 3 个独立的 bash 命令。 我有一个 python 脚本,它通过 os.system 将 bash 命令发送到命令行,但是在转到 python 脚本的下一行之前,它似乎执行了 bash 命令以完成。我只想一个接一个地向命令行发送 bash 命令。

 for i in range(0, len(core), 8) :
        os.system("bpsh "+str(1)+" nohup python /home/tree_df_unchuncked.py "+str(' '.join(core[i]))+" 0.01"+" y")
        os.system("bpsh "+str(1)+" nohup python /home/tree_df_unchuncked.py "+str(' '.join(core[i+1]))+" 0.01"+" y")
        os.system("bpsh "+str(1)+" nohup python /home/tree_df_unchuncked.py "+str(' '.join(core[i+2]))+" 0.01"+" y")

【问题讨论】:

    标签: python bash cluster-computing core supercomputers


    【解决方案1】:

    考虑从您的 python 程序构建一个 shell 脚本文件,然后执行该 shell 脚本。在构建 shell 脚本时,在每行的末尾放置一个 &。将等待命令放在程序末尾也很有用,这样 shell 脚本将暂停,直到所有后台命令(并行运行)完成。

    #/bin/bash
    a &
    b &
    c &
    wait
    

    如果你的代码被格式化,我可以给你一个更具体的答案,以便我可以复制和粘贴它,然后执行它。

    【讨论】:

      【解决方案2】:

      @Mark 是在正确的轨道上,但只是为了解释正在发生的事情:您的代码没有做任何事情来并行运行命令。这样做的方法包括但不限于:

      【讨论】:

        猜你喜欢
        • 2019-06-22
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 2013-10-26
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        • 1970-01-01
        相关资源
        最近更新 更多