如何从另一个脚本同时运行四个Python脚本

时间:2019-01-30 15:27:04

标签: python-2.7

我是Python世界的新手。我有四个Python脚本,现在在测试阶段,我必须运行不同控制台中的每个脚本。我的问题是:是否可以创建一个独特的脚本Python,并从中同时执行这4个脚本。 我正在与发布者/订阅者体系结构一起工作,所以我有一个发布者和三个订阅者。

2 个答案:

答案 0 :(得分:1)

就个人而言,我不会从蟒运行它们。创建一个批处理文件(windows)或一个bash脚本(linux)并将它们全部作为后台进程运行,这样它们就不必等待彼此完成

答案 1 :(得分:0)

在python中,您可以尝试使用类似这样的内容:

import os

# list with the name of your scripts

scripts=["script_1.py", "script_2.py","script_3.py"]

for i in range (len(scripts)):
    print "Executing ", scripts[i]
    # string with script parameters
    # in this case they are identical for everyone
    script_parameters="-p parameters"
    # build the command as if you typed it in the terminal
    #
    my_command = "python"+" "+scripts[i]+" "+script_parameters
    print my_command
    # run your command on the operating system
    os.system(my_command)

我不知道这是否是您想要的,但希望您觉得有用

相关问题