Node.js:如果不阻塞事件循环,你将如何重新创建'setTimeout'函数? [英] Node.js: How would you recreate the 'setTimeout' function without it blocking the event loop?

查看:115
本文介绍了Node.js:如果不阻塞事件循环,你将如何重新创建'setTimeout'函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多搜索,试图找出如何在Node.js中创建非阻塞代码。不幸的是,我发现的每个例子都基于一个函数,最终它已经内置了一个回调函数。所以我想用回调创建自己的函数,但由于某种原因它阻止了事件循环。这没有阻止事件循环:

I've done a lot of searching to try and find out how to create non-blocking code in Node.js. Unfortunately, every example I've found is grounded to a function that, in the end, already has a built in callback. So I wanted to create my own function with a callback, but for some reason it blocks the event loop. This didn't block the event loop:

function foo(response){
    setTimeout(function(){
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("bar");
        response.end();
    }, 7000);
}

但这样做:

function foo(response){
    function wait(callback, delay){
            var startTime = new Date().getTime();
        while (new Date().getTime() < startTime + delay);
        callback();
    }
    wait(function(){
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("bar");
        response.end();
    }, 7000);
}

我错过了非阻塞代码的哪些基本方面?

What fundamental aspect of non-blocking code am I missing?

编辑:

我重新创建setTimeout的目标更多的是我认为的心理锻炼d尝试这样我可以更好地理解均匀循环。现在我担心如果我有一些相当繁重的服务器端代码在JavaScript中进行一些原始处理,我不知道如何阻止它停止我的事件循环。

My goal to recreate setTimeout is more of a mental exercise I thought I'd try so I can understand the even loop a little better. Right now I'm worried that if I have some rather heavy server side code that's doing some raw processing in JavaScript, I won't know how to stop it from halting my event loop.

在阅读了你的答案并进一步考虑之后,我认为一个更准确的问题可能就是:如果我在我的服务器上使用JavaScript进行大量处理,我该如何阻止它中断事件循环?

After reading your answers and thinking about this a bit further, I think a more accurate question would probably be this: If I'm doing heavy processing on my server with JavaScript, how do I stop that from interrupting the event loop?

这是我第一次在这里发帖,所以我不知道我会得到什么样的回应。到目前为止,这太棒了。谢谢,伙计们。

It's my first time posting on here, so I didn't know what kind of response I was gonna get. So far, this is awesome. Thanks, guys.

编辑2:
嘿,所以再次感谢大家的建议。我最终尝试了process.nextTick,就像Raynos建议的那样......它有效!我设法用回调创建自己的非阻塞计时器。代码并不完美,但对于那些好奇的人来说,这就是它的样子:

Edit 2: Hey, so thanks again everyone for the advice. I ended up trying out process.nextTick like Raynos suggested... and it worked! I managed to create my own non-blocking timer with a callback. The code isn't perfect, but for those who are curious, this is how it looks:

var timer = {};

function delay(callback, length){
    if(!timer.startTime){
        timer.startTime = new Date().getTime();
        timer.callback = callback;
        timer.length = length;
    }
    if(new Date().getTime() < timer.startTime + timer.length){
        process.nextTick(delay);
    } else {
        timer.callback();
        timer = {};
    }
}

function list(response){
    delay(function(){
        console.log("callback");
        exec("dir", function (error, stdout, stderr) {
            response.writeHead(200, {"Content-Type": "text/plain"});
            response.write(stdout);
            response.end();
        });
    }, 7000);
}

并不打算使用此代码。但是学习如何做到这一点的过程肯定有助于我理解一些关于非阻塞的关键概念。

Not really intending to use this code. But the process of learning how to do this definitely helped me understand some key concepts about non-blocking.

对于那些仍然对非阻塞感到好奇的人,你应该看看 Raynos的文章。

And for those of you still curious about non-blocking, you should check out Raynos' article.

推荐答案

为了不阻止事件循环你的代码有最终返回事件循环并允许它继续处理。除非您的代码实际返回到事件循环,否则它不会使下一条消息出列并处理它。此代码不会在给定时间段内退出,因此永远不会将控制权返回给事件循环。

In order to not block the event loop your code has to eventually return to the event loop and allow it to continue processing. Unless your code actually returns to the event loop then it can't dequeue the next message and process it. This code won't exit for the given time period and hence never returns control to the event loop.

这篇关于Node.js:如果不阻塞事件循环,你将如何重新创建'setTimeout'函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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