为什么节点中生成的子进程在执行之前就触发关闭事件?
Why do the spawned child process in node fire close event even before getting executed?
我正在构建一个从 JSON 文件执行人偶脚本的工具。这里的挑战是实现模拟多个浏览器的并行处理,这是通过将 userAgent、高度和宽度传递给 puppeteer 实例来完成的。
但是,在生成子进程时,它们会触发带有代码 1 的关闭事件,而 compute.js 中甚至没有执行任何一行。
感谢回答和建议。
index.js 文件
const {spawn} = require('child_process');
const browserConfig = require('../config/browser-config');
// command for reference mode : npm start ref/test login
//var mode = process.argv[2];
var testScriptName = process.argv[2];
var testScriptPath;
var testScript;
var browserList;
if (testScriptName) {
console.log(testScriptName, '<------test-script');
testScriptPath = './test-scripts/' + testScriptName;
console.log(testScriptPath, '<------path');
testScript = require(testScriptPath);
browserList = testScript.start.browserList;
console.log(`browserlist --> ${browserList}`);
}
browserList.forEach(function (browser){
console.log(`browser-> ${browser}`);
let childProcess = spawn('node', ['./workers/compute.js', testScriptName, browserConfig[browser]]);
childProcess.on('close', function(code){
console.log(`Child process exited with code --> ${code}`);
});
childProcess.on('error', function(code){
console.log(`Child process errord with code --> ${code}`);
});
childProcess.on('disconnect', function(code){
console.log(`Child process disconnect with code --> ${code}`);
});
childProcess.on('message', function(message){
console.log(`Child process message --> ${message.value}`);
});
});
src/workers/compute.js文件
process.send({value: 'messsssssss'});
var testScriptName = process.argv[2];
var testScriptPath = './test-scripts/' + testScriptName;
var hostEnv = process.argv[3];
var puppeteer = require('puppeteer');
console.log(`Inside test script for hostEnv ----->${hostEnv}`);
var testScript = require(testScriptPath);
var screenCapture = require('./screenCapture.js');
var imageCounter = 0;
async function rollout(hostEnv) {
const width = hostEnv.width;
const height = hostEnv.height;
const browser = await puppeteer.launch({
headless: false,
args: [
`--window-size=${width},${height}`
],
});
const page = await browser.newPage();
await page.setUserAgent(hostEnv.userAgent);
await page.setViewport({ width, height });
return { browser, page };
}
async function boost(page, browser) {
var configObj = testScript;
var startUrl = configObj.start.open;
await page.goto(startUrl, { "waitUntil": "networkidle0" });
await screenCapture.captureImage(page, '../../capturedImages/' + testScriptName + '/' + imageCounter + '.png');
imageCounter++;
await processArray(configObj.then, page, browser);
}
async function processArray(array, page, browser) {
for (const item of array) {
await executeStep(item, page);
}
await browser.close();
}
async function executeStep(element, page) {
if (element.inputAction === 'text') {
await page.type(element.inputElement, element.inputValue, { delay: 50 });
} else if (element.inputAction === 'click') {
console.log('clicking on ', element.inputElement);
await page.click(element.inputElement);
}
if (element.waitFor) {
await page.waitForSelector(element.waitFor);
}
if (element.screenShotArea.length > 0) {
var div = element.screenShotArea[0];
await screenCapture.captureImage(page, '../../capturedImages/' + testScriptName + '/' + imageCounter + '.png', div);
imageCounter++;
}
console.log('.....................')
}
var {page, browser} = rollout(testScriptPath, hostEnv);
boost(page, browser);
索引文件产生了多个新的 child_processes 但所有进程都以代码 1 结束,我得到的只是如果 browserList 数组有 3 个元素:
Child process exited with code --> 1
Child process exited with code --> 1
Child process exited with code --> 1
这里想要的结果:compute.js 代码应该针对不同的浏览器并行执行。
这里的问题是我的节点父进程正在关闭 post 生成子进程,因为它无事可做。父进程的关闭使子进程也关闭,而没有执行它们应该完成的任何工作。
以上是获得这些的原因:
Child process exited with code --> 1
Child process exited with code --> 1
Child process exited with code --> 1
我正在构建一个从 JSON 文件执行人偶脚本的工具。这里的挑战是实现模拟多个浏览器的并行处理,这是通过将 userAgent、高度和宽度传递给 puppeteer 实例来完成的。
但是,在生成子进程时,它们会触发带有代码 1 的关闭事件,而 compute.js 中甚至没有执行任何一行。
感谢回答和建议。
index.js 文件
const {spawn} = require('child_process');
const browserConfig = require('../config/browser-config');
// command for reference mode : npm start ref/test login
//var mode = process.argv[2];
var testScriptName = process.argv[2];
var testScriptPath;
var testScript;
var browserList;
if (testScriptName) {
console.log(testScriptName, '<------test-script');
testScriptPath = './test-scripts/' + testScriptName;
console.log(testScriptPath, '<------path');
testScript = require(testScriptPath);
browserList = testScript.start.browserList;
console.log(`browserlist --> ${browserList}`);
}
browserList.forEach(function (browser){
console.log(`browser-> ${browser}`);
let childProcess = spawn('node', ['./workers/compute.js', testScriptName, browserConfig[browser]]);
childProcess.on('close', function(code){
console.log(`Child process exited with code --> ${code}`);
});
childProcess.on('error', function(code){
console.log(`Child process errord with code --> ${code}`);
});
childProcess.on('disconnect', function(code){
console.log(`Child process disconnect with code --> ${code}`);
});
childProcess.on('message', function(message){
console.log(`Child process message --> ${message.value}`);
});
});
src/workers/compute.js文件
process.send({value: 'messsssssss'});
var testScriptName = process.argv[2];
var testScriptPath = './test-scripts/' + testScriptName;
var hostEnv = process.argv[3];
var puppeteer = require('puppeteer');
console.log(`Inside test script for hostEnv ----->${hostEnv}`);
var testScript = require(testScriptPath);
var screenCapture = require('./screenCapture.js');
var imageCounter = 0;
async function rollout(hostEnv) {
const width = hostEnv.width;
const height = hostEnv.height;
const browser = await puppeteer.launch({
headless: false,
args: [
`--window-size=${width},${height}`
],
});
const page = await browser.newPage();
await page.setUserAgent(hostEnv.userAgent);
await page.setViewport({ width, height });
return { browser, page };
}
async function boost(page, browser) {
var configObj = testScript;
var startUrl = configObj.start.open;
await page.goto(startUrl, { "waitUntil": "networkidle0" });
await screenCapture.captureImage(page, '../../capturedImages/' + testScriptName + '/' + imageCounter + '.png');
imageCounter++;
await processArray(configObj.then, page, browser);
}
async function processArray(array, page, browser) {
for (const item of array) {
await executeStep(item, page);
}
await browser.close();
}
async function executeStep(element, page) {
if (element.inputAction === 'text') {
await page.type(element.inputElement, element.inputValue, { delay: 50 });
} else if (element.inputAction === 'click') {
console.log('clicking on ', element.inputElement);
await page.click(element.inputElement);
}
if (element.waitFor) {
await page.waitForSelector(element.waitFor);
}
if (element.screenShotArea.length > 0) {
var div = element.screenShotArea[0];
await screenCapture.captureImage(page, '../../capturedImages/' + testScriptName + '/' + imageCounter + '.png', div);
imageCounter++;
}
console.log('.....................')
}
var {page, browser} = rollout(testScriptPath, hostEnv);
boost(page, browser);
索引文件产生了多个新的 child_processes 但所有进程都以代码 1 结束,我得到的只是如果 browserList 数组有 3 个元素:
Child process exited with code --> 1
Child process exited with code --> 1
Child process exited with code --> 1
这里想要的结果:compute.js 代码应该针对不同的浏览器并行执行。
这里的问题是我的节点父进程正在关闭 post 生成子进程,因为它无事可做。父进程的关闭使子进程也关闭,而没有执行它们应该完成的任何工作。
以上是获得这些的原因:
Child process exited with code --> 1
Child process exited with code --> 1
Child process exited with code --> 1