我得到以下警告:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace: 
    at EventEmitter.<anonymous> (events.js:139:15)
    at EventEmitter.<anonymous> (node.js:385:29)
    at Server.<anonymous> (server.js:20:17)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1514:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1410:22)
    at TCP.onread (net.js:354:27)

我在server.js中写了这样的代码:

http.createServer(
    function (req, res) { ... }).listen(3013);

如何解决这个问题?


当前回答

直到今天我开始监视咕噜咕噜的时候,我才开始这样做。最终由

watch: {
  options: {
    maxListeners: 99,
    livereload: true
  },
}

烦人的信息消失了。

其他回答

感谢RLaaa给了我一个如何解决警告的真正问题/根本原因的想法。在我的例子中,是MySQL有bug的代码。

假设你写了一个承诺,里面的代码是这样的:

pool.getConnection((err, conn) => {

  if(err) reject(err)

  const q = 'SELECT * from `a_table`'

  conn.query(q, [], (err, rows) => {

    conn.release()

    if(err) reject(err)

    // do something
  })

  conn.on('error', (err) => {

     reject(err)
  })
})

注意,代码中有一个conn.on('error')侦听器。代码一遍又一遍地添加监听器取决于调用查询的次数。 同时if(err) reject(err)做同样的事情。

所以我删除了conn.on('error')监听器,瞧…解决了! 希望这对你有所帮助。

将.on()替换为once()。使用once()在同一函数处理事件时删除事件监听器。

如果这不能解决问题,那么在package.json中重新安装restler :“restler git: / / github.com/danwrong/restler.git # 9 d455ff14c57ddbe263dbbcd0289d76413bfe07d”

这与restler 0.10对node的错误行为有关。你可以在这里看到git关闭的问题:https://github.com/danwrong/restler/issues/112 然而,npm还没有更新这个,所以这就是为什么你必须引用git头。

缺省情况下,任何单个事件最多可以注册10个侦听器。

如果这是你的代码,你可以通过:

const emitter = new EventEmitter()
emitter.setMaxListeners(100)
// or 0 to turn off the limit
emitter.setMaxListeners(0)

但如果这不是你的代码,你可以使用技巧来增加全局默认限制:

require('events').EventEmitter.prototype._maxListeners = 100;

当然,你可以关闭限制,但要小心:

// turn off limits by default (BE CAREFUL)
require('events').EventEmitter.prototype._maxListeners = 0;

顺便说一句。代码应该在应用程序的最开始。

ADD:从节点0.11开始,这段代码也可以改变默认限制:

require('events').EventEmitter.defaultMaxListeners = 0

You said you are using process.on('uncaughtException', callback); Where are you executing this statement? Is it within the callback passed to http.createServer?If yes, different copy of the same callback will get attached to the uncaughtException event upon each new request, because the function (req, res) { ... } gets executed everytime a new request comes in and so will the statement process.on('uncaughtException', callback);Note that the process object is global to all your requests and adding listeners to its event everytime a new request comes in will not make any sense. You might not want such kind of behaviour. In case you want to attach a new listener for each new request, you should remove all previous listeners attached to the event as they no longer would be required using: process.removeAllListeners('uncaughtException');

正如其他人所指出的那样,提高限额并不是最好的答案。我也面临着同样的问题,但在我的代码中,我没有使用任何事件侦听器。当我仔细研究代码时,我有时会创建很多承诺。每个承诺都有一些抓取所提供URL的代码(使用一些第三方库)。如果你正在做类似的事情,那么它可能是原因。

在使用ES6的Promise.all()时,限制并发性的最佳方法是什么?