如何使用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作为子进程,并订阅其stdout和stderr流以实时获取数据(而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);
});
相关文章:
- 子进程未接收消息
- 如何通过子进程连接Java应用程序后在node.js中编写文件
- 节点.js子进程中的运行函数
- 如何将画布对象从子进程流式传输回 Node.js
- 如何在Firefox插件中向子进程发送消息,如Chrome原生消息
- 从生成的子进程中获取信息的最佳方法
- 在 Node.js 中使用 Jasmine 测试子进程发送
- 在快速服务器中生成子进程
- 如何在 node.js 中关闭子进程的 stdio 管道
- 如何使用并行子进程在大型数组上执行“工作”
- 使用 node.js (子进程) 运行 shell 脚本
- 如何从nodejs中的守护进程生成gpg子进程
- Cloud9 在生成子进程时抛出错误,是否有解决方法
- 如何在从 Meteor.method 返回之前等待子进程结果
- 将输入数据发送到节点.js中的子进程
- 完成时的子进程事件
- 如何在 Debian 上通过 node.js 中的引用变量停止子进程
- Node.js为什么不立即启动子进程
- Gulp调试-管道到进程.stdout给出TypeError('无效数据')
- 如何使用node.js查看phantomjs子进程的stdout