The Simple and Powerful Parallel Runner for Windows Scripting Hosts


In here, we have presented a simple and powerful Parallel Job Runner written in C#. Based on similar idea, we present a parallel job (script) runner written purely in Javascript. Actually, it is the Microsoft JScript that runs on the Windows Scripting Host (WSH) environment.

The idea of running an external script/program under WSH is through the WScript.Shell com object. The Run method launches the external program asynchronously, so it means that the main script won’t await the return of the program, which is perfect for running multiple jobs in parallel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// written by https://helloacm.com
(function(args, runner) {
    if (args.length == 0) {
        // cscript.exe //Nologo Runner.js --wscript --//Nologo --//T:5 job1.vbs job2.js
        throw "job1.js job2.js job3.vbs job4.vbs ...";
    }   
    var param = "";
    var interpreter = "cscript.exe";
    for (var i = 0; i < args.length; i ++) {
        var cur = args(i);
        //  Microsoft JScript does not support startWith function
        if ((cur.length >= 2) && (cur.charAt(0) == '-') && (cur.charAt(1) == '-')) {
            cur = cur.substring(2).toLowerCase();
            if (cur == 'cscript') {
                interpreter = "cscript.exe";
            } else if (cur == 'wscript') {
                interpreter = "wscript.exe";
            } else {
                param += cur + " ";
            }
        } else {
            // run external script asynchronously so in other words in parallel
            runner.Run(interpreter + " " + param + " " + cur);
        }
    }
})(WScript.Arguments, new ActiveXObject("WScript.Shell"));
// written by https://helloacm.com
(function(args, runner) {
	if (args.length == 0) {
		// cscript.exe //Nologo Runner.js --wscript --//Nologo --//T:5 job1.vbs job2.js
		throw "job1.js job2.js job3.vbs job4.vbs ...";
	}	
	var param = "";
	var interpreter = "cscript.exe";
	for (var i = 0; i < args.length; i ++) {
		var cur = args(i);
		//  Microsoft JScript does not support startWith function
		if ((cur.length >= 2) && (cur.charAt(0) == '-') && (cur.charAt(1) == '-')) {
			cur = cur.substring(2).toLowerCase();
			if (cur == 'cscript') {
				interpreter = "cscript.exe";
			} else if (cur == 'wscript') {
				interpreter = "wscript.exe";
			} else {
				param += cur + " ";
			}
		} else {
			// run external script asynchronously so in other words in parallel
			runner.Run(interpreter + " " + param + " " + cur);
		}
	}
})(WScript.Arguments, new ActiveXObject("WScript.Shell"));

Now, let’s create two jobs e.g. job1.vbs and job2.js. Both contains a single Line:

1
2
3
4
// job1.vbs
WScript.Echo "Hello from job1.vbs"
// job2.js
WScript.Echo ("Hello from job2.js");
// job1.vbs
WScript.Echo "Hello from job1.vbs"
// job2.js
WScript.Echo ("Hello from job2.js");
RunnerJs The Simple and Powerful Parallel Runner for Windows Scripting Hosts jscript Task Parallel Library windows scripting host

Parallel Runner in Javascript

As we can see, both scripts run at the same time, and here, we can specify the additional parameters (with double dashes) e.g. Timeout, NoLogo etc. Both dialogs will disappear as the maximum allowed timeout is set to five seconds via //T:5.

However if you specify the interpreter (host) with the value of cscript.exe (via –cscript or –//H:CScript), both jobs will run and finish quickly after outputing message to their own console (separate window).

Currently, only *.vbs or *.js are supported (under WSH) in this tiny tool. However, you can easily modify the above script runner, which allows the customization of interpreter.

Please note that running multiple processes is generally known as multitasking. In order to utilize all cores, your tasks (processes) are required to make your core work parallel.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
521 words
Last Post: VBScript: How to Modify the Priority of a Running Process?
Next Post: Review of Seagate 2TB Hybrid SSHD Hard Drive

The Permanent URL is: The Simple and Powerful Parallel Runner for Windows Scripting Hosts

Leave a Reply