process.exit(0):输出消失? [英] process.exit(0): output disappears?

查看:195
本文介绍了process.exit(0):输出消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Emacs lisp命令调用 node hello.js

Emacs lisp command calling node hello.js:

(call-process "node" nil t nil "hello.js")

两种变体 hello.js


  • hello_1.js

  • hello_1.js:

console.log('Hello world!');

输出:

Hello world!
0


  • hello_2.js

    console.log('Hello world!');
    process.exit(5);
    

    输出(没有来自日志语句的输出!):

    Output (no output from log statement!):

    5
    


  • process.exit(5)导致输出被抑制的原因是什么?

    What is the reason that process.exit(5) causes output to be suppressed?

    注意:


    • 我在GNU Emacs 24.3.1(i386- mww-nt5.1.2600)
      2013-03-17在MARVIN 中结合使用Node.js v0.10.18 ,
      Windows XP / SP3 / 32

    • I experienced the problem in GNU Emacs 24.3.1 (i386-mingw-nt5.1.2600) of 2013-03-17 on MARVIN in combination with Node.js v0.10.18, running on Windows XP/SP3/32.

    我尝试过 EShell 执行节点命令行:no output

    I tried EShell to execute the node command line: no output

    process.exit()调用低级别 process.reallyExit(),导致问题:
    无输出

    process.exit() calls low level process.reallyExit(), which causes the problem: no output

    process.reallyExit()在C ++中实现:

    void Exit(const FunctionCallbackInfo<Value>& args) {
      HandleScope scope(node_isolate);
      exit(args[0]->IntegerValue());
    }
    

    [...]

    NODE_SET_METHOD(process, "reallyExit", Exit);
    


    推荐答案

    的输出出现:

    Phew, I figured it out myself. First of all, I discovered that a delay causes the output to appear:

    hello_3.js

    console.log('Hello world!');
    setTimeout(function () {
        process.exit(5);
    }, 1000);
    

    输出:

    Hello world!
    5
    

    所以我仔细检查了Node.js文档,用于 console 发现

    So I double checked Node.js documentation for console, and found:


    当目标是终端或
    文件时,控制台功能是同步的(以避免在过早退出的情况下避免丢失的消息),并且当
    它是一个管道(以避免长时间阻塞)。

    The console functions are synchronous when the destination is a terminal or a file (to avoid lost messages in case of premature exit) and asynchronous when it's a pipe (to avoid blocking for long periods of time).

    然后我决定确保没有使用管道并写批量脚本
    hello_2.bat

    Then I decided to make sure that no pipe is used and wrote a batch script hello_2.bat:

    @ECHO OFF
    node hello_2.js >test
    TYPE test
    

    调用时输出脚本与(call-processcmd.exenil t nil/ C
    hello_2.bat)

    Hello world!
    0
    

    (返回值为0而不是5,但我不在乎那个)

    (return value is 0 instead of 5, but I don't care about that)

    回答我的问题:


    1. 看起来像在Windows上的Emacs中,使用一个管道从程序中检索
      输出 call-process

    也提到了EShell:它似乎在Windows上似乎不被Node.js认可为
    终端,而且EShell内部可以使用 call-process
    类似于运行程序。

    As I also mentioned EShell: It does not seem to be recognized as a terminal by Node.js on Windows, and possibly EShell internally uses call-process or similar to run programs.

    检测作为标准输出目标的管道导致 console.log
    将异步运行。

    Detection of a pipe as destination for standard output causes console.log to be run asynchronously.

    process.js在Windows上似乎放弃了所有调度的
    异步任务,因此没有输出生成。

    process.exit(5) in Node.js on Windows seems to discard all scheduled asynchronous tasks, and thus no output is generated.

    这个假设得到了拼命的将输出引导到
    管道在Windows命令提示符下:

    This assumption is supported by the outcome of desperately directing output to a pipe inside the Windows command prompt:

    C:\Temp> node hello_2.js | MORE
    
    C:\Temp>
    


    最后,我发现问题是大约一年前(截至2013年9月)已知

    Finally, I found out that the issue is known since about a year ago (as of September 2013).

    这篇关于process.exit(0):输出消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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