簡體   English   中英

如何使用node.js查看phantomjs子進程的stdout?

[英]How to see stdout of a phantomjs child process using node.js?

在下面的node.js代碼中,我通常必須等待phantomjs子進程終止才能獲得stdout。 我想知道在phantomjs子進程運行時是否有任何方法可以看到stdout?

var path = require('path')
var childProcess = require('child_process')
var phantomjs = require('phantomjs')
var binPath = phantomjs.path

var childArgs = [
  path.join(__dirname, 'phantomjs-script.js'),
]

childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
  // handle results 
})

您可以spawn PhantomJS作為一個子進程,並訂閱其輸出和錯誤流來獲得實時數據(而exec僅將程序執行后緩沖結果)。

var path = require('path');
var phantomjs = require('phantomjs');
var spawn = require('child_process').spawn;

var childArgs = [
  path.join(__dirname, 'phantomjs-script.js'),
];
var child = spawn(phantomjs.path, childArgs);

child.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

child.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

child.on('close', function (code) {
  console.log('child process exited with code ' + code);
});

暫無
暫無

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

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