为什么我的 node 爬虫执行完成后,不会自动退出程序?
发布于 6 年前 作者 JZLeung 3748 次浏览 来自 问答

有使用了 async await 的,执行完成后,已经手动断开了 mongodb 的链接。 结果是链接貌似已经成功断开了,但是程序却一直没有退出,导致后续的再执行就链接不上 mongodb 了。该如何解决? image.png 这是爬虫的运行代码: image.png

10 回复

可以尝试process.exit手动退出。。。

@fruit-memory 除了这么暴力的做法,还有其他嘛?

goon什么情况下为fasle

来自酷炫的 CNodeMD

还没细看看你的代码,不过写爬虫,应该用mongo连接池的,用完一个连接释放就可以了,你这样每请求一个获取一个连接然后在断开一个连接性能太差,时间都浪费在建立连接上了

@jjeejj 难道不用关吗? 连接池该如何改?

@zswnew 一次抓完 600 页后。

@JZLeung mysql 模块中有个mysql.createPool方法

比如:

var mysql = require('mysql');
var pool  = mysql.createPool(...);

pool.getConnection(function(err, connection) {
  // Use the connection
  connection.query('SELECT something FROM sometable', function (error, results, fields) {
    // And done with the connection.
    connection.release();

    // Handle error after the release.
    if (error) throw error;

    // Don't use the connection here, it has been returned to the pool.
  });
});

具体的你可以查看文档的 mysql

process.exit()

@wxs77577 超时了,因为我有 await。

回到顶部