【问题标题】:Starting multiple instances of a python script at once from linux command line从 linux 命令行一次启动多个 python 脚本实例
【发布时间】:2011-03-06 06:20:10
【问题描述】:

我想将一段python脚本启动一千次!而不是尝试一个一个地启动它们,我怎样才能从 linux 命令行中做到这一点?

现在,我正在这样做:

nohup python test.py &
nohup python test.py &
nohup python test.py &
nohup python test.py &
nohup python test.py &
...

提前致谢。

【问题讨论】:

    标签: python linux shell command-line console


    【解决方案1】:

    作为单行,在 Bash 中:

    for i in {1..1000}; do nohup python test.py & done
    

    【讨论】:

      【解决方案2】:

      我建议您将生成逻辑保留在 Python 程序中。也许使用multiprocessing 库来完成这些过程。如果您要在 bash 中生成它们,那么如果没有一些重要的脚手架,将很难管理所有这些。

      【讨论】:

        【解决方案3】:

        最简单的方法是使用 shell 脚本创建一个循环,这对任何事情都有效:

        #!/bin/bash
        X=0
        COUNT=1000
        while [ $X -lt $COUNT ]; do
            nohup python test.py &
            X=$((X+1))
        done
        

        【讨论】:

        • for 循环会更简单。 for i in {1..1000}for ((i=0; i<1000; i++)) 或在 Bourne shell 中:for i in $(seq 1000)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-03
        • 2012-09-16
        • 2019-07-17
        • 1970-01-01
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多