因此,我在后端项目上看到了一些线程阻塞操作。我调试了每一行,以查看只有猫鼬查询才被线程阻塞。我尝试了文档中的所有内容,但由于某种原因,猫鼬查询一直阻塞线程...
这是一个示例代码,证明了我的观点
require('dotenv').config();
require("./models/Model1");
const blocked = require('blocked'),
assert = require('assert'),
mongoose = require('mongoose'),
Model1 = mongoose.model("Model1");
mongoose.connect("mongodb://localhost:27017/" + process.env.DATABASE, {useNewUrlParser: true});
mongoose.Promise = Promise;
//mongoose.Promise = global.Promise;
// Tried both of the options above, didn't worked.
mongoose.connection.on('connected', () => {
console.log("Connected to MongoDB!");
setTimeout(threadBlockingTest, 1500);
}).on('error', (err) => {
console.error("MongoDB connection error: " + err.message);
process.exit();
});
async function threadBlockingTest() {
let promise = Model1.findOne({_id: "uhT416tcypesZiT"}).exec();
assert.ok(promise instanceof Promise);
promise.then(function (doc) {
// Do something with the response
});
}
blocked(function (ms) {
console.error('BLOCKED FOR %sms', ms | 0);
});
输出为
已连接到MongoDB!
封锁了92ms
其中线程阻塞时间为80-130ms。我尝试同时运行多个查询,以查看更多查询被阻止了。
我也试图注释threadBlockingTest()的内部。线程从未在所有尝试中被阻塞。