Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多处理检测多个子进程的终止_Python_Multiprocessing - Fatal编程技术网

Python 使用多处理检测多个子进程的终止

Python 使用多处理检测多个子进程的终止,python,multiprocessing,Python,Multiprocessing,如果name='main': #!/usr/bin/python import os from os import getpid import multiprocessing build="613719" file1=open('/auto/home/venkam11/python/install-script/build-ddr-file.txt', 'r') def installation(model,ddr,build): cli = "/auto/tools/qa/

如果name='main':

#!/usr/bin/python
import os

from os import getpid

import multiprocessing


build="613719"
file1=open('/auto/home/venkam11/python/install-script/build-ddr-file.txt', 'r')

def installation(model,ddr,build):

    cli = "/auto/tools/qa/shared/qa-branch/util/install.pl -durham -restart -silentinstall -model %s -branch 6.2A %s %s"  %(model, ddr, build)

    print cli
    os.popen2(cli)
    print "installation has started on %s \n" %ddr
基本上,我试图读取具有机器名的文件,并使用多处理,我想在我已读取的机器上安装操作系统

上面是我的代码,当我运行时,它立即开始在所有机器上安装,主程序终止

但是我希望主程序不要终止,它必须等到子进程完成作业,并返回输出,说明子进程作业已完成

安装make take anytime 1小时或2小时,但我希望消息显示所有流程作业都已完成


任何人都可以在此提供帮助。

填充进程列表,并使用
join()
将它们与父进程连接起来,然后打印消息。这样,父级在执行后面的行之前,等待子级完成任务

同样的代码应该如下所示:

pid=getpid()

print("parent process id :{}".format(getpid()))

for ddr in file1:
    print ddr.rstrip()
    if 'dd4500' in ddr:
        print "dd4500"
        model = "dd4500"
    elif ('apollo' or 'apolloplus') in ddr:
        print "dd9500"
        model = "dd9500"
    elif 'dd2500' in ddr:
        print "dd2500"
        model = "dd2500"
    elif 'dd7200' in ddr:
        print "dd7200"
        model = "dd7200"
    elif 'jupiter' in ddr:
        print "dd9800"
        model = "dd9800"
    ddr = ddr.rstrip()
    ins=multiprocessing.Process(target=installation, args=(model,ddr,build))
    ins.start()

欢迎来到Stackoverflow。与线程非常相似,与子进程同步的最简单方法是
join()
它们,通常是在创建线程/进程时。当引入
子流程
模块时,
os.popen2
调用被弃用,因为它没有对已启动的子流程提供必要程度的控制和通信。为此,您可以使用
多处理
模块

使用
多处理
对您的问题的技术答案已经在另一个答案以及中详细介绍,其中解释了调用实体如何与其子进程同步。例如,通过将每个子流程存储在一个列表中来处理未知数量的子流程是完全允许的,如下所示

#!/usr/bin/python
import os
import multiprocessing
import subprocess
import time
import sys

from os import getpid

file1 = open('/auto/home/venkam11/python/install-script/build-ddr-file.txt', 'w+')
print ("enter the ddr names one by one in each line and press ctrl-d twice")
userInput = sys.stdin.readlines()
file1.writelines(userInput)
file1.close()
build = input("\nenter the build number : \n")
branch = raw_input("enter the branch name  : " )
file1 = open('/auto/home/venkam11/python/install-script/build-ddr-file.txt', 'r')

def installation(model, branch, ddr, build, shared_dict):
        cli = "/auto/tools/qa/shared/qa-branch/util/install.pl -durham -restart -silentinstall -model %s -branch %s %s %s"  %(model, branch, ddr, build)
        print cli
        print "installation has started on %s \n" % ddr
        time.sleep(20)
        try:
            subprocess.check_call(cli, shell=True)
            shared_dict[ddr] = True
        except subprocess.CalledProcessError:
            shared_dict[ddr] = False

if __name__ == '__main__':
    #pid=getpid()
    #print("parent process id : {}".format(getpid()))
    processes = []
    manager = multiprocessing.Manager()
    shared_dict = manager.dict()
    for ddr in file1:
        print ddr.rstrip()
        if 'dd4500' in ddr:
            print "dd4500"
            model = "dd4500"
        elif ('apollo' or 'apolloplus') in ddr:
            print "dd9500"
            model = "dd9500"
        elif 'dd2500' in ddr:
            print "dd2500"
            model = "dd2500"
        elif 'dd7200' in ddr:
            print "dd7200"
            model = "dd7200"
        elif 'jupiter' in ddr:
            print "dd9800"
            model = "dd9800"

        ddr = ddr.rstrip()
        ins = multiprocessing.Process(target=installation, args=(model, branch, ddr, build, shared_dict))
        ins.start()
        processes.append(ins)

    for process in processes:
        process.join()

    print('All the installations are complete')
    print('Details: ')
    for ddr, success in shared_dict.items():
        if success:
            print('Installation on {} successful'.format(ddr))
        else:
            print('Installation on {} unsuccessful'.format(ddr))

一个更有用的答案可能是允许创建和监视其他进程,而不需要
多处理
的开销和复杂性,它具有进程间通信的高级功能,而您似乎并不需要这些功能。它的文档中有很多很好的例子,说明了如何将较旧和/或不太合适的Python代码调整为使用新模块,因此值得一看。

Hi Vignesh,它对我有用。我使用了你告诉我的步骤,但没有放在正确的地方。非常感谢您的帮助。嗨,Vignesh,还有一种方法可以读取进程的输出,比如安装日志。因此,我应该阅读日志,检查日志中的成功或失败消息,并在机器安装成功或失败时打印。正如您所说,您可以将状态写入日志文件,并在退出父级时从中读取。但是在这里,您必须从perl脚本写入文件,读取部分可以在python脚本中完成。或者,正如@holdenweb所说,您可以使用
子流程
,具体地说,
检查调用
以查看它是否成功执行,如果没有,则打印失败消息。嗨,Vignesh,它可以工作,但是,在一台机器上安装可能会更快,而在另一台机器上安装则需要更多时间。所以问题是,一旦安装结束,它会显示如下消息[2019-02-08 08:11:58]调试:日志文件保存到/tmp/install-dd4500-103-613719_16.Log[2019-02-08 08:11:58]调试:关闭控制台会话[2019-02-08 08:11:58]调试:安装成功,返回退出代码0。Perl已退出活动线程:0正在运行并取消连接1已完成并取消连接0在dd4500-103.datadomain.com上的运行和分离安装成功,感谢您的回答。我还有一个问题。另外,是否有一种方法可以读取进程的输出,如安装日志。因此,我应该阅读日志,检查日志中的成功或失败消息,并在机器安装成功或失败时打印。对于这类事情,您肯定应该搜索
子流程
示例(我想可能会有很多来自stackoverflow的示例)。但是请先看文档中的例子,因为里面有很多好主意。嗨,谢谢你的回答。我将通读这些例子。
for p in my_processes:
    p.join()