NodeJs 中的多个客户端请求 [英] Multiple Client Requests in NodeJs

查看:89
本文介绍了NodeJs 中的多个客户端请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我点击/route2 ,我会在 15 秒后得到响应,但在此期间/route1 会立即给出响应.服务器不应该等待 15 秒然后给/route1 响应.

If i hit /route2 , i get reponse 15s later,but during that time /route1 gives response immediately. Shouldn't server wait for 15s and then give the response to /route1.

我阅读了 https://medium.com/@cinish/nodejs-multiple-client-requests-694d6353218b 但没办法.

I read https://medium.com/@cinish/nodejs-multiple-client-requests-694d6353218b but could not the idea.

我的理解是/route2 被击中控制台 P1 然后因为有 setTimeout() ,把它放在外线程中,然后控制台 P2 然后作为 setTimeout() ,把它放在外线程中.现在等待 setTimeouts() 到结束.

What i understood is as /route2 is hit console P1 then as there is setTimeout() ,put it in external thread,then console P2 then as setTimeout() ,put it in external thread.Now wait for setTimeouts() to finish.

(此时事件循环一定很忙,因为它在等待 p1 和 p2 执行,所以它应该不接受新的客户端请求).但确实如此.为什么?

(At this time the event loop must be busy as it waiting for p1 and p2 fulfillment, so it should accept no new client request). But it does. Why?

app.get("/route1", (req, res) => {
    res.send("Route1");
});


app.get("/route2", async (req, res) => {

    const p1 = new Promise((resolve, reject) => {
        console.log("P1");
        setTimeout(() => {
            console.log(5);
            resolve();
        }, 15000);
    })
    const p2 = new Promise((resolve, reject) => {
        console.log("P2")
        setTimeout(() => {
            console.log(1);
            resolve();
        }, 1000);
    });

    const ans = await Promise.all([p1, p2]);
    res.send("Route2");
})

推荐答案

setTimeout() 是非阻塞".这意味着它设置计时器,立即从 setTimeout() 返回,然后继续执行其余的代码.

setTimeout() is "non-blocking". That means it sets the timer, returns immediately from the setTimeout() and then continues to execute the rest of the code.

因此,在您的情况下,/route2 创建两个承诺,设置两个计时器,然后等待 Promise.all() 完成.在它遇到 await 时,您的 async 路由处理程序返回一个承诺并将控制权返回给事件循环.

So, in your case /route2 creates the two promises, sets two timers and then waits for Promise.all() to finish. At the point it hits the await, your async route handler returns a promise and returns control back to the event loop.

因此,一旦 /route1 请求到达,它就可以被处理了./route2 在两个计时器都完成后才处于活动状态.

So, as soon as the /route1 request arrives, it's ready to be processed. /route2 is not active until both timers are done.

然后,当更长的计时器准备好触发时,下次 JS 解释器返回事件循环检查是否还有其他事情要做时,它将看到该计时器,处理该计时器回调,该计时器回调将解析p1 承诺,然后导致 Promise.all() 被解析.

Then, when the longer timer is ready to fire, the next time the JS interpreter goes back to the event loop to check for anything else to do, it will see that timer, process the timer callback and that timer callback will resolve the p1 promise and then cause the Promise.all() to be resolved.

如果我点击/route2 ,我会在 15 秒后得到响应,但在此期间/route1 会立即给出响应.服务器不应该等待 15 秒然后响应/route1.

If i hit /route2 , i get reponse 15s later,but during that time /route1 gives response immediately. Shouldn't server wait for 15s and then give reponse to /route1.

正如关于 setTimeout() 的解释是非阻塞的,所以当 /route2 处理程序正在等待两个计时器触发时,nodejs 可以自由处理其他事件(就像传入的 /route1 事件一样).

As explained about setTimeout() is non-blocking so while the /route2 handler is waiting for the two timers to fire, nodejs is free to process other events (like the incoming /route1 event).

我的理解是/route2 被击中控制台 P1 然后因为有 setTimeout() ,把它放在外线程中,然后控制台 P2 然后作为 setTimeout() ,把它放在外线程中.现在等待 setTimeouts() 到结束.

What i understood is as /route2 is hit console P1 then as there is setTimeout() ,put it in external thread,then console P2 then as setTimeout() ,put it in external thread.Now wait for setTimeouts() to finish.

定时器不在外部线程中运行.它们是 nodejs 事件循环中有点独特的设计.计时器按照到期时间的顺序存储在一个排序的链表中.每次 JS 解释器回到事件循环并进入事件循环的计时器部分时,它只是检查链表中最前面的计时器,看看它的时间是否到了.如果是这样,它会触发该事件.如果没有,它就会去寻找其他类型的事件.

Timers are not run in external threads. They are a somewhat unique design in the nodejs event loop. Timers are stored in a sorted linked list in the order of when they are due. Each time the JS interpreter gets back to the event loop and gets to the timer section of the event loop, it just checks the front most timer in the linked list to see if its time has arrived. If so, it triggers that event. If not, it just goes and looks for other types of events.

最终,如果事件循环无所事事而进入休眠状态,它将休眠适当的时间,以便及时唤醒计划运行的下一个计时器(如果在此之前没有其他事情将其唤醒).

Eventually, if the event loop has nothing to do and goes to sleep, it will sleep for an appropriate time to wake up in time for the next timer scheduled to run (if nothing else wakes it up before that).

(此时事件循环一定很忙,因为它在等待 p1 和 p2 满载,所以它应该不接受新的客户端请求).但确实如此.为什么?

(At this time the event loop must be busy as it waiting for p1 and p2 fullfillment,so it should accept no new client request). But it does.Why?

这是您的主要错误假设.此时,事件循环完全可以自由地处理其他事件.设置了两个计时器,事件循环将在它们的时间到期时运行它们的回调,但在此之前事件循环将处理任何其他传入事件,例如 /route1 请求.

This is your main wrong assumption. At this time, the event loop is entirely free to process other events. The two timers are set and the event loop will run their callbacks when their time is due, but until then the event loop will process any other incoming events like the /route1 request.

这篇关于NodeJs 中的多个客户端请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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