Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs中的event.on()和event.once()的区别_Node.js_Child Process_Eventemitter - Fatal编程技术网

Node.js nodejs中的event.on()和event.once()的区别

Node.js nodejs中的event.on()和event.once()的区别,node.js,child-process,eventemitter,Node.js,Child Process,Eventemitter,我正在测试plus_one应用程序,在运行它时,我只是想澄清一下我对event.once()和event.on()的概念 这是plus_one.js > process.stdin.resume(); process.stdin.on('data',function(data){ var number; try{ number=parseInt(data.toString(),10); number+=1; process.s

我正在测试plus_one应用程序,在运行它时,我只是想澄清一下我对event.once()和event.on()的概念

这是plus_one.js

> process.stdin.resume();
process.stdin.on('data',function(data){
    var number;
    try{
        number=parseInt(data.toString(),10);
        number+=1;
        process.stdout.write(number+"\n");
        }
    catch(err){
        process.stderr.write(err.message+"\n");
        }
    });
var spawn=require('child_process').spawn;
var child=spawn('node',['plus_one.js']);

setInterval(function(){
    var number=Math.floor(Math.random()*10000);
    child.stdin.write(number+"\n");
    child.stdout.on('data',function(data){
        console.log('child replied to '+number+' with '+data);
        });
    },1000);
这是test_plus_one.js

> process.stdin.resume();
process.stdin.on('data',function(data){
    var number;
    try{
        number=parseInt(data.toString(),10);
        number+=1;
        process.stdout.write(number+"\n");
        }
    catch(err){
        process.stderr.write(err.message+"\n");
        }
    });
var spawn=require('child_process').spawn;
var child=spawn('node',['plus_one.js']);

setInterval(function(){
    var number=Math.floor(Math.random()*10000);
    child.stdin.write(number+"\n");
    child.stdout.on('data',function(data){
        console.log('child replied to '+number+' with '+data);
        });
    },1000);
在使用child.stdin.on()时,我很少收到maxlistener偏移警告,但在使用child.stdin.once()时,情况并非如此,为什么会发生这种情况


是因为child.stdin正在侦听以前的输入吗?但是在这种情况下,应该更频繁地设置maxlistener偏移量,但它在一分钟内只发生一次或两次。

使用
EventEmitter.on()
,您会附加一个完整的侦听器,而使用
EventEmitter.once()
时,它是一个一次性侦听器,在触发一次后会分离。根据最新的官方文件,只触发一次的侦听器不会计入最大侦听器数。

。.once()侦听器不计入maxlisteners

emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', () => {
  // do stuff
  emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
});