簡體   English   中英

為什么節點中生成的子進程甚至在執行之前就會觸發關閉事件?

[英]Why do the spawned child process in node fire close event even before getting executed?

我正在構建一個從 JSON 文件執行 puppeteer 腳本的工具。 這里的挑戰是實現模擬多個瀏覽器的並行處理,這是通過將 userAgent、高度和寬度傳遞給 puppeteer 實例來完成的。

但是,在生成子進程時,它們會使用代碼 1 觸發 close 事件,甚至沒有在 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 代碼應該針對不同的瀏覽器並行執行。

這里的問題是我的節點父進程正在關閉生成子進程的帖子,因為它無事可做。 父進程的關閉使子進程也關閉,而沒有執行它們應該完成的任何工作。

以上是得到這些的原因:

Child process exited with code --> 1
Child process exited with code --> 1
Child process exited with code --> 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM