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

查看:38
本文介绍了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.

请参见下面的屏幕截图.

See screenshot below.

正确的输出

我稍微挖掘了 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 时,它在中间被调用.

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.stdout process.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.

如果要确保仅在所有 stdout 完成后才处理 stderr ,则可能需要听 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 的结果,也许您可​​以考虑尝试

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天全站免登陆