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

标签 python multiprocessing

#!/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

如果名称 == '主要':

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()

基本上我正在尝试读取具有计算机名称并使用多处理的文件,我想在我已读取的计算机上安装操作系统。

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

但我希望主程序不要终止,它必须等到子进程完成作业,并返回表示子进程作业完成的输出。

安装 make take 1 小时或 2 小时的任何时间,但我想要一条消息说所有进程作业都已完成。

任何人都可以帮忙吗?

最佳答案

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

其代码应如下所示:

#!/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 - 使用多处理检测多个子进程的终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54589940/

相关文章:

python - 将数据帧附加到多处理目标函数之外的列表

python - .read() 在 python 中使用askopenfilename() 返回 "unicode object has no attribute read"

Python 运行 ffmpeg 并进行多处理

python - 在 Python 中返回偶数和奇数

python - 散点图python双边缘线

python - 使用多处理python将元素添加到列表

python - 使用 python 在 Linux 中合并文件时文件大小大大减少

python - 在 Python 中分别读取多个 CSV 并将它们并行保存到数据帧字典中

python - 如何使用内置函数在列表中找到最接近目标的值?

python - 如何将项目放回 queue.Queue