简体   繁体   English

多次使用子进程和调用脚本来并行运行-如何检查返回码是否有错误

[英]Using subprocess and calling script many times to run in parallel - how do I check return codes for any errors

Below is code section which loops through all the namespaces and then calls remove_empty_dirs for each one. 下面的代码部分循环遍历所有命名空间,然后为每个命名空间调用remove_empty_dirs

remove_empty_dirs uses subprocess to call a separate python script for each namespace. remove_empty_dirs使用子remove_empty_dirs为每个命名空间调用单独的python脚本。 Using a counter we call up to 25 subprocesses at a time. 使用计数器,我们一次最多调用25个子进程。

I use a ps -ef command to check if the subprocess script is still running. 我使用ps -ef命令检查子进程脚本是否仍在运行。

I need to use some kind of p=subprocess . 我需要使用某种p=subprocess I think to be able to check on the return code for each execution , but how do I capture up to 25 process objects at once? 我认为能够检查每次执行的返回代码,但是如何一次捕获多达25个过程对象?

And what would I use instead of ps -ef to monitor when subprocess scripts are finished? 我将用什么代替ps -ef来监视子进程脚本何时完成?

import os, sys, datetime, time, glob, re, commands
import subprocess
from sets import Set

def get_all_namespaces_run_remove_empty_dirs(filename):
    """
    Creates set of unique namespaces from applid.namespace.ctl file and calls remove_empty_dirs for each one.
    """

    print "Namespaces to be processed: ", sorted(namespaces)
    for name in sorted(namespaces):
        remove_empty_dirs(name)
        job_ct = 99
        while job_ct > 25:
            cmd = "ps -ef | grep archive_remove_empty_dirs.py | grep -vE 'grep|sh -c' | wc -l"
            status,job_ct = commands.getstatusoutput(cmd)
            print job_ct, " jobs are running"
            job_ct = int(job_ct)
            if job_ct > 25:
                print "waiting for jobs to finish"
                time.sleep(30)

def wait_for_subprocesses_to_end():

    time_ct = 0
    job_ct = 99
    while job_ct > 0:
        cmd = "ps -ef | grep archive_remove_empty_dirs.py | grep -vE 'grep|sh -c' | wc -l"
        status,job_ct = commands.getstatusoutput(cmd)
        job_ct = int(job_ct)
        if job_ct > 0:
            time_ct +=1
            if time_ct > 5:
                logging.info("%d jobs are still running" % job_ct)
                cmd = "ps -ef | grep archive_remove_empty_dirs.py | grep -vE 'grep|sh -c'"
                status,output = commands.getstatusoutput(cmd)
                print output
                time_ct = 0
            time.sleep(10)
    return

def remove_empty_dirs(NS_PREFIX):
    """
    executes archive_remove_empty_dirs.py to remove empty directories for each NS_PREFIX
    """

    global statistics, NS_SITE, app_state

    output_filename = "%s/%s_remove_empty_dirs.out" % (archivelog_dir,NS_PREFIX)
    err_filename = "%s/%s_remove_empty_dirs.err" % (archivelog_dir,NS_PREFIX)
    cmd = "%s/archive_remove_empty_dirs.py" % source_dir

    print "Executing %s %s %s" % (cmd, NS_PREFIX, NS_SITE)
    subprocess.Popen([cmd, NS_PREFIX, NS_SITE],
            stdout=open(output_filename, 'w'),
            stderr=open(err_filename, 'w'),
            preexec_fn=os.setpgrp
            )
    logging.info(NS_PREFIX + " removing empty directories...")

I solved this by 我解决了

def get_all_namespaces_run_remove_empty_dirs(filename): def get_all_namespaces_run_remove_empty_dirs(文件名):

global master_ix

namespaces is a list of 50-100 items - call remove_empty_dirs for each one until 26 are called then call wait_for_subprocesses_to_end until less than 26 are running 名称空间是50至100个项目的列表-分别调用remove_empty_dirs直到调用26个,然后调用wait_for_subprocesses_to_end直到运行少于26个

for name in sorted(namespaces):
    remove_empty_dirs(name)
    while master_ix > 25:
        wait_for_subprocesses_to_end()
        time.sleep(10)

def wait_for_subprocesses_to_end(): def wait_for_subprocesses_to_end():

this is the solution - the process_list is built by the function remove_empty_dirs and now we loop through this list getting the return code when it completes and removing it from the list. 这是解决方案-process_list由remove_empty_dirs函数构建,现在我们遍历此列表,以获取完成时的返回码并将其从列表中删除。

global subprocess_failures, master_ix
j = len(process_list)
print "begin wait_for_subprocesses_to_end length of process_list = %d" % j
i = 0
while i < j:
    check_process, NS_PREFIX = process_list[i]
    ret_code = check_process.poll()
    #print i, ret_code
    if ret_code == None:
        i += 1
        continue
    else:
        del process_list[i]
        j -= 1
        if ret_code != 0:
            subprocess_failures +=1
            print "%s failure!!! rc=%d" % (NS_PREFIX, ret_code)

master_ix = len(process_list)
print "end wait_for_subprocesses_to_end length of process_list = %d" % master_ix
print "subprocess_failures = %d" % subprocess_failures
return

def remove_empty_dirs(NS_PREFIX): def remove_empty_dirs(NS_PREFIX):

executes archive_remove_empty_dirs.py to remove empty directories for each NS_PREFIX 执行archive_remove_empty_dirs.py删除每个NS_PREFIX的空目录

this is the solution - save the process object (p) in process_list - each entry in process_list is a tuple of p and unique identifier of process 这是解决方案-将过程对象(p)保存在process_list中-process_list中的每个条目都是p的元组和过程的唯一标识符

global statistics, NS_SITE, app_state, master_ix

output_filename = "%s/%s_remove_empty_dirs.out" % (archivelog_dir,NS_PREFIX)
err_filename = "%s/%s_remove_empty_dirs.err" % (archivelog_dir,NS_PREFIX)
cmd = "%s/archive_remove_empty_dirs.py" % source_dir

print "Executing %s %s %s" % (cmd, NS_PREFIX, NS_SITE)
p = subprocess.Popen([cmd, NS_PREFIX, NS_SITE],
        stdout=open(output_filename, 'w'),
        stderr=open(err_filename, 'w'),
        preexec_fn=os.setpgrp
        )
process_list.append((p,NS_PREFIX))
master_ix += 1
logging.info("%d.  %s removing empty directories..." % (master_ix, NS_PREFIX))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将脚本作为并行子进程运行 - Run Script as parallel Subprocess 如何在子进程中运行 function 和 arguments 而不直接调用它? - How do I run a function with arguments in a subprocess without calling it directly? 使用Pandoc和Django的HTML to Context模板:如何避免多次调用子流程? - HTML to Context template using Pandoc and Django: how to avoid calling subprocess too many times? 如何使用子进程模块从python脚本运行AVL(二进制)? - How do I run AVL (a binary) from a python script using the subprocess module? 使用线程并行运行子进程 - Using threading to run a subprocess in parallel 使用多进程和子进程在python中运行并行Stata do文件 - Run parallel Stata do files in python using multiprocess and subprocess 如何将在不同时间获取的许多输入返回到 excel? - How do I return many inputs taken at different times into excel? 如何在 linux 中与不同的参数并行运行 python 脚本? - How do I run a python script in parallel with different arguments in linux? 如何在Python中同时或并行执行此操作(检查请求状态代码)? - How to do this (check requests status codes) in Python concurrently or in parallel? 如何使用 subprocess 模块将 csh 脚本转换为 python? - How do I convert a csh script to python using the subprocess module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM