多任务并行
将任务结果输出到临时文件中然后收集起来实现多任务同时执行输出。
通过文件名来确认任务完成状态
// 参数:文件列表,最大同时进行任务数
ArrageTask(files, 3);
// 参数:文件列表,最大同时进行任务数
function ArrageTask(paraCmdList, paramiMaxMul)
{
var sQuery = "Select * from Win32_Process where Name = 'cmd.exe'";
var WshShell = new ActiveXObject("WScript.Shell");
var WMI = GetObject("WinMgmts:");
var objProcesses;
do
{
objProcesses = null;
WScript.Sleep(200);
objProcesses = WMI.ExecQuery(sQuery);
while(objProcesses.count < paramiMaxMul && paraCmdList.length != 0)
{
WshShell.Run("cmd /c " + paraCmdList);
paraCmdList.splice(0, 1);
objProcesses = WMI.ExecQuery(sQuery);
}
}
while(objProcesses.count != 0 || paraCmdList.length != 0)
}
====================================version 1=============================================
var objStartDate = new Date(); var strStartDate = objStartDate.getYear().toString(); strStartDate += "-" + ((objStartDate.getMonth()+1).toString().length < 2 ? "0" : "") + (objStartDate.getMonth()+1).toString(); strStartDate += "-" + (objStartDate.getDate().toString().length < 2 ? "0" : "") + objStartDate.getDate().toString(); strStartDate += " " + (objStartDate.getHours().toString().length < 2 ? "0" : "") + objStartDate.getHours().toString(); strStartDate += ":" + (objStartDate.getMinutes().toString().length < 2 ? "0" : "") + objStartDate.getMinutes().toString(); strStartDate += ":" + (objStartDate.getSeconds().toString().length < 2 ? "0" : "") + objStartDate.getSeconds().toString(); // 打印开始日期 WScript.StdOut.WriteLine("StartTime:" + strStartDate); String.prototype.Trim = function(){ return this.replace(/(^s*)|(s*$)/g, ""); } String.prototype.LTrim = function(){ return this.replace(/(^s*)/g, ""); } String.prototype.Rtrim = function(){ return this.replace(/(s*$)/g, ""); } var fso = new ActiveXObject("Scripting.FileSystemObject"); var objPathListFile = fso.OpenTextFile("InputFile", 1 , false); var files = new Array(); while (!objPathListFile.AtEndOfStream) { var strLine = objPathListFile.ReadLine(); strLine = strLine.Trim(); if((strLine != "") && (strLine.charAt(0) != "\\")) { var i = 0; for(; i < files.length; i++) { if(files[i] == strLine) { break; } } if(i == files.length) { files.push(strLine); } } } objPathListFile.Close(); var WshShell = new ActiveXObject("WScript.Shell"); var WMI = GetObject("WinMgmts:"); var sProcessorQuery = "Select * from Win32_Processor"; var iMaxMul = 3; // 看看电脑有几个核 var objProcessors = WMI.ExecQuery(sProcessorQuery); var pk = new Enumerator(objProcessors); for (; !pk.atEnd(); pk.moveNext()) { var objProcessor = pk.item(); var prok = new Enumerator(objProcessor.Properties_); for(; !prok.atEnd(); prok.moveNext()) { var objProp = prok.item(); if(objProp.Name == "NumberOfCores") { iMaxMul += objProp.Value; } } } var strResultNamePre = "Success"; var sCmdQuery = "Select * from Win32_Process where Name = 'cmd.exe'"; // 取得根文件夹 var strRootDir = WScript.ScriptFullName.substring(0, WScript.ScriptFullName.length - WScript.ScriptName.length - 1); var objRootDir = fso.GetFolder(strRootDir); ArrageTask(files, iMaxMul); // 收集打印已完成的结果 function ShowFinishedFiles() { var files = new Enumerator(objRootDir.Files); var cmds = WMI.ExecQuery(sCmdQuery); var cmdsK = new Enumerator(cmds); for (; !files.atEnd(); files.moveNext()) { var objFile = files.item(); var strSubFileName = objFile.Name; if(strSubFileName.substring(0, strResultNamePre.length) != strResultNamePre) { continue; } // 看看文件是不是没有进程在写 cmdsK.moveFirst(); for (; !cmdsK.atEnd(); cmdsK.moveNext()) { var bIs = false; var cmdPropertyK = new Enumerator(cmdsK.item().Properties_); for(; !cmdPropertyK.atEnd(); cmdPropertyK.moveNext()) { var cmdProperty = cmdPropertyK.item(); if(cmdProperty.Name != "CommandLine") { continue; } // 貌似有些进程对出时间CommandLine变成了NULL,所以判断一哈 if(cmdProperty.Value != null && cmdProperty.Value.substring(cmdProperty.Value.length - strSubFileName.length - 1, cmdProperty.Value.length - 1) == strSubFileName) { bIs = true; } break; } if(bIs) { break; } } if(!cmdsK.atEnd()) { // 还没用完呢 continue; } var objTextStream = objFile.OpenAsTextStream(1/*只读*/, -2/*默认编码*/); while (!objTextStream.AtEndOfStream) { WScript.StdOut.WriteLine(objTextStream.ReadLine()); } objTextStream.Close(); objFile.Delete(); } } // 结束时打印所有结果 function ShowAllFiles() { // 打印结果 filesK = new Enumerator(objRootDir.Files); for (; !filesK.atEnd(); filesK.moveNext()) { var objResultFile = filesK.item(); var strSubFileName = objResultFile.Name; if(strSubFileName.substring(0, strResultNamePre.length) != strResultNamePre) { continue; } var objTextStream = objResultFile.OpenAsTextStream(1/*只读*/, -2/*默认编码*/); while (!objTextStream.AtEndOfStream) { WScript.StdOut.WriteLine(objTextStream.ReadLine()); } objTextStream.Close(); objResultFile.Delete(); } } // 参数:文件列表,最大同时进行任务数 function ArrageTask(files, paramiMaxMul) { var sExeQuery = "Select * from Win32_Process where Name = 'singleprocess.exe'"; var iNo = 0; do { while(WMI.ExecQuery(sExeQuery).count < paramiMaxMul && files.length != 0) { var strResultName = strResultNamePre + iNo; iNo = iNo + 1; // 将识别的结果名称传入 WshShell.Run("singleprocess.bat" + " " + files[0] + " " + ">" + strResultName, 0); files.splice(0, 1); } // 休息一会儿 WScript.Sleep(300); ShowFinishedFiles(); } while(WMI.ExecQuery(sExeQuery).count != 0) WScript.Sleep(100); WScript.Sleep(1000); ShowAllFiles(); } // 打印结束日期 var objFinishDate = new Date(); var strFinishDate = objFinishDate.getYear().toString(); strFinishDate += "-" + ((objFinishDate.getMonth()+1).toString().length < 2 ? "0" : "") + (objFinishDate.getMonth()+1).toString(); strFinishDate += "-" + (objFinishDate.getDate().toString().length < 2 ? "0" : "") + objFinishDate.getDate().toString(); strFinishDate += " " + (objFinishDate.getHours().toString().length < 2 ? "0" : "") + objFinishDate.getHours().toString(); strFinishDate += ":" + (objFinishDate.getMinutes().toString().length < 2 ? "0" : "") + objFinishDate.getMinutes().toString(); strFinishDate += ":" + (objFinishDate.getSeconds().toString().length < 2 ? "0" : "") + objFinishDate.getSeconds().toString(); WScript.StdOut.WriteLine("FinishTime:" + strFinishDate);
====================================version 2=============================================
查找要处理的文件列表
@ECHO OFF setlocal ENABLEDELAYEDEXPANSION set CURRENT_DIR=%CD% ::设置工作目录 set WORK_PATH=.\work IF EXIST "%WORK_PATH%\InputFile" ( del "%WORK_PATH%\InputFile" ) ::遍历文件列表 FOR /F "eol=/delims=" %%i in (%WORK_PATH%\filelist.txt) do ( set tmp=%%i set fileorfolder=!tmp:%%CURRENT_DIR%%=%CURRENT_DIR%! FOR /f "delims=" %%j in ('dir /s /b /A:-D !fileorfolder!') do ( IF EXIST "%%j" (echo %%j >>%WORK_PATH%\InputFile) ) ) IF EXIST "%WORK_PATH%\InputFile" ( cd "%WORK_PATH%" cscript.exe /nologo /d mulity2.js ) exit
多任务执行脚本
var objStartDate = new Date(); var strStartDate = objStartDate.getYear().toString(); strStartDate += "-" + ((objStartDate.getMonth()+1).toString().length < 2 ? "0" : "") + (objStartDate.getMonth()+1).toString(); strStartDate += "-" + (objStartDate.getDate().toString().length < 2 ? "0" : "") + objStartDate.getDate().toString(); strStartDate += " " + (objStartDate.getHours().toString().length < 2 ? "0" : "") + objStartDate.getHours().toString(); strStartDate += ":" + (objStartDate.getMinutes().toString().length < 2 ? "0" : "") + objStartDate.getMinutes().toString(); strStartDate += ":" + (objStartDate.getSeconds().toString().length < 2 ? "0" : "") + objStartDate.getSeconds().toString(); // 打印开始日期 WScript.StdOut.WriteLine("StartTime:" + strStartDate); String.prototype.Trim = function(){ return this.replace(/(^s*)|(s*$)/g, ""); } String.prototype.LTrim = function(){ return this.replace(/(^s*)/g, ""); } String.prototype.Rtrim = function(){ return this.replace(/(s*$)/g, ""); } var fso = new ActiveXObject("Scripting.FileSystemObject"); var objPathListFile = fso.OpenTextFile("InputFile", 1 , false); var files = new Array(); while (!objPathListFile.AtEndOfStream) { var strLine = objPathListFile.ReadLine(); strLine = strLine.Trim(); if((strLine != "") && (strLine.charAt(0) != "\\")) { var i = 0; for(; i < files.length; i++) { if(files[i] == strLine) { break; } } if(i == files.length) { files.push(strLine); } } } objPathListFile.Close(); var WMI = GetObject("WinMgmts:"); var sProcessorQuery = "Select * from Win32_Processor"; var iMaxMul = 3; // 看看电脑有几个核 var objProcessors = WMI.ExecQuery(sProcessorQuery); var pk = new Enumerator(objProcessors); for (; !pk.atEnd(); pk.moveNext()) { var objProcessor = pk.item(); var prok = new Enumerator(objProcessor.Properties_); for(; !prok.atEnd(); prok.moveNext()) { var objProp = prok.item(); if(objProp.Name == "NumberOfCores") { iMaxMul += objProp.Value; } } } ArrageTask(files, iMaxMul); // 参数:文件列表,最大同时进行任务数 function ArrageTask(files, paramiMaxMul) { var WshShell = new ActiveXObject("WScript.Shell"); var tasks = new Array(); do { for(var i = 0; i < tasks.length; ) { if(tasks[i].Status != 0) { WScript.StdOut.Write(tasks[i].StdOut.ReadAll()); tasks.splice(i, 1); } else { ++i } } while(tasks.length < paramiMaxMul && files.length > 0) { // 将识别的结果名称传入 tasks.push(WshShell.Exec("singleprocess.exe" + " " + files[0])); files.splice(0, 1); } // 休息一会儿 WScript.Sleep(300); } while(tasks.length > 0) } // 打印结束日期 var objFinishDate = new Date(); var strFinishDate = objFinishDate.getYear().toString(); strFinishDate += "-" + ((objFinishDate.getMonth()+1).toString().length < 2 ? "0" : "") + (objFinishDate.getMonth()+1).toString(); strFinishDate += "-" + (objFinishDate.getDate().toString().length < 2 ? "0" : "") + objFinishDate.getDate().toString(); strFinishDate += " " + (objFinishDate.getHours().toString().length < 2 ? "0" : "") + objFinishDate.getHours().toString(); strFinishDate += ":" + (objFinishDate.getMinutes().toString().length < 2 ? "0" : "") + objFinishDate.getMinutes().toString(); strFinishDate += ":" + (objFinishDate.getSeconds().toString().length < 2 ? "0" : "") + objFinishDate.getSeconds().toString(); WScript.StdOut.WriteLine("FinishTime:" + strFinishDate);
=======================================================================
@ECHO OFF setlocal ENABLEDELAYEDEXPANSION set PRJ_DIR=%CD% rem set I=.. rem set I='%%~pI' rem echo !I! ::设置工作目录 set WORK_PATH=.\work IF EXIST "%WORK_PATH%\InputFile" ( del "%WORK_PATH%\InputFile" ) set existfile= ::遍历文件列表 FOR /F "eol=/delims=" %%i in (%WORK_PATH%\filelist.txt) do ( set tmp=%%i set fileorfolder=!tmp:%%PRJ_DIR%%=%PRJ_DIR%! set time_string=%TIME% set existfile=a FOR /f "delims=" %%j in ('dir /s /b /A:-D !fileorfolder!') do ( IF EXIST "%%j" ( set existfile=b echo %%j >>%WORK_PATH%\InputFile ) ) IF !existfile!==a ( echo !tmp! error echo !fileorfolder! ) ) IF EXIST "%WORK_PATH%\InputFile" ( cd "%WORK_PATH%" cscript.exe /nologo /d mulity2.js "%WORK_PATH%\InputFile" ) exit
function WriteLog(str) { objLogFile.WriteLine(str); objLastLogFile.WriteLine(str); } function WriteConsole(str) { WriteLog(str); WScript.StdOut.WriteLine(str); } var fso = new ActiveXObject("Scripting.FileSystemObject"); var objLogFile = fso.OpenTextFile("log.txt", 8 , true); var objLastLogFile = fso.OpenTextFile("lastlog.txt", 2 , true); var objStartDate = new Date(); //objStartDate.getVarDate()-objStartDate.getVarDate() var strStartDate = objStartDate.getYear().toString(); strStartDate += "-" + ((objStartDate.getMonth()+1).toString().length < 2 ? "0" : "") + (objStartDate.getMonth()+1).toString(); strStartDate += "-" + (objStartDate.getDate().toString().length < 2 ? "0" : "") + objStartDate.getDate().toString(); strStartDate += " " + (objStartDate.getHours().toString().length < 2 ? "0" : "") + objStartDate.getHours().toString(); strStartDate += ":" + (objStartDate.getMinutes().toString().length < 2 ? "0" : "") + objStartDate.getMinutes().toString(); strStartDate += ":" + (objStartDate.getSeconds().toString().length < 2 ? "0" : "") + objStartDate.getSeconds().toString(); // 打印开始日期 WriteConsole("StartTime:" + strStartDate); String.prototype.Trim = function(){ return this.replace(/(^s*)|(s*$)/g, ""); } String.prototype.LTrim = function(){ return this.replace(/(^s*)/g, ""); } String.prototype.Rtrim = function(){ return this.replace(/(s*$)/g, ""); } var aryError = new Array(); function RecordError(str) { var reError = /(Error|Warning|Info)\s(\d+)\:/gim; while(reError.exec(str)) { aryError.push(RegExp.$2); } } var objPathListFile = fso.OpenTextFile("File", 1 , false); var files = new Array(); while (!objPathListFile.AtEndOfStream) { var strLine = objPathListFile.ReadLine(); strLine = strLine.Trim(); if((strLine != "") && (strLine.charAt(0) != "\\")) { var i = 0; for(; i < files.length; i++) { if(files[i] == strLine) { break; } } if(i == files.length) { files.push(strLine); } } } objPathListFile.Close(); var WMI = GetObject("WinMgmts:"); var sProcessorQuery = "Select NumberOfCores, NumberOfLogicalProcessors from Win32_Processor"; var iMaxMul = 1; // 看看电脑有几个核 var objProcessors = WMI.ExecQuery(sProcessorQuery); if(objProcessors.Count == 1) { var numberOfCore = new Number(objProcessors.ItemIndex(0).Properties_.Item("NumberOfCores")); var numberOfLogicalProcessor = new Number(objProcessors.ItemIndex(0).Properties_.Item("NumberOfLogicalProcessors")); iMaxMul = numberOfCore * numberOfLogicalProcessor - 1; } var deadTestInterval = 30 * 1000; var WshShell = new ActiveXObject("WScript.Shell"); ArrageTask(files, iMaxMul); function SetPriorityLow(processId) { var sCmdQuery = "Select * from Win32_Process where ProcessId = " + processId; var cmds = WMI.ExecQuery(sCmdQuery); if(cmds.Count != 1) { return 0; } /* 64 低 32 标准 128 高 256 实时 Idle 低 Below Normal 低于标准 Normal 标准 Above Normal 高于标准 High Priority 高 Realtime 实时 */ try { cmds.ItemIndex(0).SetPriority(64); } catch (e) { WriteConsole("Process" + processId + " setpriority failed"); } } function GetCPUTime(processId) { var sCmdQuery = "Select KernelModeTime, UserModeTime from Win32_Process where ProcessId = " + processId; var cmds = WMI.ExecQuery(sCmdQuery); if(cmds.Count != 1) { return 0; } var kernelModeTime = new Number(cmds.ItemIndex(0).Properties_.Item("KernelModeTime").Value); var userModeTime = new Number(cmds.ItemIndex(0).Properties_.Item("UserModeTime").Value); //debugger; return (kernelModeTime + userModeTime); } // 参数:文件列表,最大同时进行任务数 function ArrageTask(files, paramiMaxMul) { function Task(time, file, taskinfo) { this.m_cpuTime = 0; this.m_time = time; this.m_file = file; this.m_info = taskinfo; } var tasks = new Array(); do { var currentTime = (new Date()).getVarDate(); //WScript.StdOut.WriteLine("tasknum:" + tasks.length); for(var i = 0; i < tasks.length; ) { if(tasks[i].m_info.Status != 0) { var strOutput = tasks[i].m_info.StdOut.ReadAll(); RecordError(strOutput) WriteConsole(strOutput); tasks.splice(i, 1); } else { //WScript.StdOut.WriteLine("file" + tasks[i].m_file + ":" + tasks[i].m_info.Status); if((currentTime - tasks[i].m_time) > deadTestInterval) { //WScript.StdOut.WriteLine(GetCPUTime(tasks[i].m_info.ProcessId)); //debugger; //WScript.StdOut.WriteLine("Process" + " " + tasks[i].m_file + " " + "too" + " " + "long!!"); tasks[i].m_time = currentTime; // 触发一下,不然会有些任务死掉,不明原因 var cpuTime = GetCPUTime(tasks[i].m_info.ProcessId); if(cpuTime > tasks[i].m_info.m_cpuTime) { tasks[i].m_info.m_cpuTime = cpuTime; } else { //tasks[i].m_info.StdOut.ReadAll(); //tasks[i].m_info.Terminate(); // 不占CPU的任务了,直接读取等它死了 var strOutput = tasks[i].m_info.StdOut.ReadAll(); RecordError(strOutput) WriteConsole(strOutput); } } ++i } } while(tasks.length < paramiMaxMul && files.length > 0) { // 将识别的结果名称传入 var process = WshShell.Exec("singleprocess.exe" + " " + files[0]); SetPriorityLow(process.ProcessId); tasks.push(new Task((new Date()).getVarDate(), files[0], process)); // 将识别的结果名称传入 process = WshShell.Exec("singleprocess2.exe" + " " + files[0]); SetPriorityLow(process.ProcessId); tasks.push(new Task((new Date()).getVarDate(), files[0], process)); files.splice(0, 1); } // 休息一会儿 WScript.Sleep(50); } while(tasks.length > 0) } var strError = "Error:" + aryError.length; WriteConsole(strError); // 打印结束日期 var objFinishDate = new Date(); var strFinishDate = objFinishDate.getYear().toString(); strFinishDate += "-" + ((objFinishDate.getMonth()+1).toString().length < 2 ? "0" : "") + (objFinishDate.getMonth()+1).toString(); strFinishDate += "-" + (objFinishDate.getDate().toString().length < 2 ? "0" : "") + objFinishDate.getDate().toString(); strFinishDate += " " + (objFinishDate.getHours().toString().length < 2 ? "0" : "") + objFinishDate.getHours().toString(); strFinishDate += ":" + (objFinishDate.getMinutes().toString().length < 2 ? "0" : "") + objFinishDate.getMinutes().toString(); strFinishDate += ":" + (objFinishDate.getSeconds().toString().length < 2 ? "0" : "") + objFinishDate.getSeconds().toString(); WriteConsole("FinishTime:" + strFinishDate); //} objLogFile.Close(); objLastLogFile.Close();