process.stdout.on( 'data', ... ) 和 process.stderr.on( 'data', ... ) 的顺序 [英] Order of process.stdout.on( 'data', ... ) and process.stderr.on( 'data', ... )

查看:16
本文介绍了process.stdout.on( 'data', ... ) 和 process.stderr.on( 'data', ... ) 的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个电子应用程序,这个应用程序处理从电子内部执行的终端命令.

I'm writing an electron app and this app deals with terminal commands executed from within electron.

我在执行 npm ls 命令时遇到问题.从 cli 运行它时,依赖关系树会打印到 stdout,最后可能会出现一些来自 stderr 的警告.

I've got troubles with the execution of the npm ls command. When running it from the cli the dependency tree is printed to stdout and right at the end some warning might appear coming from stderr.

请看下面的截图.

正确输出

我稍微挖掘了 npm 源代码,它首先将结果记录下来,然后打印错误.所以它就像我在终端中看到的一样.

I digged npm source code a bit and it logs out the results first and afterwards it prints errors. So it's exactly like I see it in my terminal.

但是,当我对 child_process spawn (或 exec 没关系)执行相同操作时,顺序不同.

However when I do the same with child_process spawn ( or exec it doesn't matter ), the order is different.

困惑输出

看起来由于 stdout 数据的大块,stderr 打印在所有 stdout 的中间.

It looks like because of the big chunks of stdout data the stderr is printed right in the middle of all the stdout.

下面我写的代码:

// this is mapped to require( 'child_process' ).spawn
this.$set( 'process', this.spawn(
  'npm',
  [ 'ls' ],
  {
    cwd   : options.cwd,

    // following are only my tryouts - nothing helped :(

    // some npm ls command destroy kill the scripts
    // with too big buffers in stdout
    // maxBuffer : 1024 * 5000
    // shell : true
  }
) );

// this.handleData is only printing out for nwo
this.process.stdout.on( 'data', this.handleData );
this.process.stderr.on( 'data', this.handleData );

当大数据来自 stdout 和来自 stderr 的小数据时,stderr 会在中间被调用.

It seams when large data comes from stdout and tiny data coming from stderr that stderr somehow gets called in the middle.

这是预期的行为吗?我可以以某种方式解决此问题以检索与终端中相同的行为吗?

Is this expected behavior? Can I work around this somehow to retrieve the same behavior as in my terminal?

谢谢.:)

推荐答案

process.stdoutprocess.stderr 不保证以任何特定顺序发出数据彼此,所以正如您所注意到的,只要任一管道中有任何数量的数据,都可能会调用您的回调.

process.stdout and process.stderr are not guaranteed to emit data in any particular order relative to each other, so as you've noticed, your callback may be called whenever there's any amount of data in either pipe.

如果你想确保 stderr 仅在所有 stdout 完成后处理,你可能想听 stdout.on('end', cb) 并且只在回调 cb 中调用 stderr.on('data', this.handleData).

If you want to make sure that the stderr is handled only once all of stdout is finished, you might want to listen to stdout.on('end', cb) and only call stderr.on('data', this.handleData) in that callback cb.

也就是说,如果您只是想要 npm ls 的结果,也许您可​​以考虑尝试 以编程方式使用 npm 模块?文档并不令人惊奇,但它是一个更高级别的 API.

That said, if you just want the results of npm ls, perhaps you might consider trying to use the npm module programmatically? The documentation isn't amazing, but it's a higher-level API.

这篇关于process.stdout.on( 'data', ... ) 和 process.stderr.on( 'data', ... ) 的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆