multiprocessing—Python并发执行多个cmd命令

python如何并发执行多个函数?

  • 使用该包实现多进程执行cmd命令:multiprocessing
  • 代码目的:解决bat脚本并行运行cmd命令时无法生成日志问题,用python实现并行执行cmd命令并生成日志。
import os
import re
import time
import datetime
from multiprocessing import Process     #导multiprocessing包


def cz_part_one():
    time.sleep(1)
    #将运行的cmd命令存放在列表里
    cmd_list = [
        "F:\\111\\chengshang\\0530\\javaForWindowsx64\\jre\\bin\\java.exe -Xms512m -Xmx1024m -jar  F:\\111\\chengshang\\0530\\javaForWindowsx64\\autoCompile.jar -c F:\\111\\chengshang\\0530\\javaForWindowsx64,subSystem=authGate_DEVELOP,product=XM-TEST,subSysVersioniterVersion=1auth-shijian-IT,productVersion=EMP,source_code_path=project_path:F:\\111\\lanyuan-notebook-3.1v-first##class_path:F:\\111\\lanyuan-notebook-3.1v-first  F:\\111\\chengshang\\0530\\javaForWindowsx64\\ComplierPath1.xml",
        "F:\\111\\chengshang\\0530\\javaForWindowsx64\\jre\\bin\\java.exe -Xms512m -Xmx1024m -jar  F:\\111\\chengshang\\0530\\javaForWindowsx64\\autoCompile.jar -c F:\\111\\chengshang\\0530\\javaForWindowsx64,subSystem=authGate_DEVELOP,product=XM-TEST,subSysVersioniterVersion=1auth-shijian-IT,productVersion=EMP,source_code_path=project_path:F:\\111\\lanyuan-notebook-3.1v-second##class_path:F:\\111\\lanyuan-notebook-3.1v-second  F:\\111\\chengshang\\0530\\javaForWindowsx64\\ComplierPath2.xml",
        "F:\\111\\chengshang\\0530\\javaForWindowsx64\\jre\\bin\\java.exe -Xms512m -Xmx1024m -jar  F:\\111\\chengshang\\0530\\javaForWindowsx64\\autoCompile.jar -c F:\\111\\chengshang\\0530\\javaForWindowsx64,subSystem=lebank_DEVELOP,product=XM-TEST,subSysVersioniterVersion=3lebank-shijian-IT,productVersion=EMP,source_code_path=project_path:F:\\111\\lanyuan-notebook-3.1v-first2##class_path:F:\\111\\lanyuan-notebook-3.1v-first2  F:\\111\\chengshang\\0530\\javaForWindowsx64\\ComplierPath5.xml"]
    for cmd in cmd_list:    #遍历列表
        dtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S')         #获取系统当前时间
        kcmd = re.sub('shijian', dtime, cmd)         #使用正则替换cmd命令中的变量参数
        print(kcmd)                    #kcmd是要执行的命令,这样打印是为了观察与cz_part_one函数是否是并行执行命令
        with os.popen(kcmd, "r") as p:       #执行命令
            r = p.read()       #r是执行结果,可输出打印到控制台
            subSysVersioniterVersion = kcmd.split(',')[3].split('=')[-1]
            file_handle = open("F:\\111\\chengshang\\1\\" + subSysVersioniterVersion + ".txt",
                               mode='a+')  # mode=a+  可读可追加  mode=w 可写并覆盖原来内容  F:\\111\\chengshang\\1\\:生成路径
            file_handle.write(str(r) + "\n")      #将控制台执行结果保存当txt文件
            file_handle.close()

def cz_part_two():
    time.sleep(2)
    cmd_list = [
        "F:\\111\\chengshang\\0530\\javaForWindowsx64\\jre\\bin\\java.exe -Xms512m -Xmx1024m -jar  F:\\111\\chengshang\\0530\\javaForWindowsx64\\autoCompile.jar -c F:\\111\\chengshang\\0530\\javaForWindowsx64,subSystem=mobilebank_DEVELOP,product=XM-TEST,subSysVersioniterVersion=1mobi2lebank-shijian-IT,productVersion=EMP,source_code_path=project_path:F:\\111\\lanyuan-notebook-3.1v-first1##class_path:F:\\111\\lanyuan-notebook-3.1v-first1  F:\\111\\chengshang\\0530\\javaForWindowsx64\\ComplierPath3.xml",
        "F:\\111\\chengshang\\0530\\javaForWindowsx64\\jre\\bin\\java.exe -Xms512m -Xmx1024m -jar  F:\\111\\chengshang\\0530\\javaForWindowsx64\\autoCompile.jar -c F:\\111\\chengshang\\0530\\javaForWindowsx64,subSystem=mobilebank_DEVELOP,product=XM-TEST,subSysVersioniterVersion=1mobi2lebank-shijian-IT,productVersion=EMP,source_code_path=project_path:F:\\111\\lanyuan-notebook-3.1v-second1##class_path:F:\\111\\lanyuan-notebook-3.1v-second1  F:\\111\\chengshang\\0530\\javaForWindowsx64\\ComplierPath4.xml",
        "F:\\111\\chengshang\\0530\\javaForWindowsx64\\jre\\bin\\java.exe -Xms512m -Xmx1024m -jar  F:\\111\\chengshang\\0530\\javaForWindowsx64\\autoCompile.jar -c F:\\111\\chengshang\\0530\\javaForWindowsx64,subSystem=lebank_DEVELOP,product=XM-TEST,subSysVersioniterVersion=3lebank-shijian-IT,productVersion=EMP,source_code_path=project_path:F:\\111\\lanyuan-notebook-3.1v-second2##class_path:F:\\111\\lanyuan-notebook-3.1v-second2  F:\\111\\chengshang\\0530\\javaForWindowsx64\\ComplierPath6.xml"]
    for cmd in cmd_list:
        dtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        kcmd = re.sub('shijian',dtime,cmd)
        print(kcmd)
        with os.popen(kcmd, "r") as p:
            r = p.read()
            subSysVersioniterVersion = kcmd.split(',')[3].split('=')[-1]
            file_handle = open("F:\\111\\chengshang\\2\\" + subSysVersioniterVersion + ".txt",
                               mode='a+')  # mode=a+  可读可追加  mode=w 可写并覆盖原来内容
            file_handle.write(str(r) + "\n")
            file_handle.close()

if __name__ == "__main__":
	#实现并行执行2个函数
    Process(target=cz_part_one).start()       
    Process(target=cz_part_two).start()


 注:
     2个函数下面设置的间隔时间适用于解决执行程序错乱问题,若无此场景可删

你可能感兴趣的:(cmd,multiprocessing,并行,python,命令模式,自动化)