首页 \ 问答 \ Node.js是否真的异步执行后台I / O任务?(Does Node.js really do the background I/O tasks asynchronously?)

Node.js是否真的异步执行后台I / O任务?(Does Node.js really do the background I/O tasks asynchronously?)

编辑:我们可以关闭。 是不是真的异步,非阻塞javascript不可能?


var PATH = require ("path");
var URL = require ("url");

var sleep = function (ms){
    var start = new Date ().getTime ();
    while ((new Date ().getTime () - start) < ms);
}

require ("http").createServer (function (req, res){
    if (URL.parse (req.url).pathname === "/1"){
        console.log ("tab 1: I'm in");
        PATH.exists ("test", function (exists){

            sleep (5000);

            res.writeHead (200, {"Content-Type": "text/plain"});
            res.end ("1");
            console.log ("tab 1: I'm done");
        });
    }else{
        console.log ("tab 2: I'm in");
        res.writeHead (200, {"Content-Type": "text/plain"});
        res.end ("2");
        console.log ("tab 2: I'm done");
    }
}).listen (80);
  1. 将内容复制到文件中。
  2. 执行文件。
  3. 在浏览器中打开一个新选项卡。 将url设置为localhost/1 。 不要走了。
  4. 在浏览器中打开一个新选项卡。 将url设置为localhost/2 。 不要走了。
  5. 返回第一个标签。 按Enter键并在更改到第二个选项卡后立即按Enter键。

结果:

  • 控制台日志:

    表1:我在
    表1:我已经完成了
    表2:我在
    表2:我已经完成了

  • 标签1等待5秒钟以接收结果“1”。

  • 标签2也必须等待5秒钟,因为标签1正在睡眠5秒钟。

文档说除了代码之外所有都是异步的。 只有一个线程。 一次只能有一个请求。 请求已入队。

I / O调用应该是异步的,对吧? 那么,如果回调来自异步I / O进程,为什么选项卡2必须等待选项卡1?

谢谢。


Edit: We can close. Isn't truly asynchronous, non-blocking javascript impossible?


var PATH = require ("path");
var URL = require ("url");

var sleep = function (ms){
    var start = new Date ().getTime ();
    while ((new Date ().getTime () - start) < ms);
}

require ("http").createServer (function (req, res){
    if (URL.parse (req.url).pathname === "/1"){
        console.log ("tab 1: I'm in");
        PATH.exists ("test", function (exists){

            sleep (5000);

            res.writeHead (200, {"Content-Type": "text/plain"});
            res.end ("1");
            console.log ("tab 1: I'm done");
        });
    }else{
        console.log ("tab 2: I'm in");
        res.writeHead (200, {"Content-Type": "text/plain"});
        res.end ("2");
        console.log ("tab 2: I'm done");
    }
}).listen (80);
  1. Copy the content into a file.
  2. Execute the file.
  3. Open a new tab in browser. Set the url to localhost/1. Don't go yet.
  4. Open a new tab in browser. Set the url to localhost/2. Don't go yet.
  5. Go back to the first tab. Press enter and immediately after change to the second tab and press enter.

Result:

  • console log:

    tab 1: I'm in
    tab 1: I'm done
    tab 2: I'm in
    tab 2: I'm done

  • Tab 1 waits 5 seconds to receive the result "1".

  • Tab 2 also has to wait 5 seconds because tab 1 is sleeping for 5 seconds.

The docs says that all is asynchronous except the code. Only one thread. Only one request at a time. Requests are enqueued.

I/O calls are supposed to be asynchronous, right? Then why tab 2 has to wait to tab 1 if the callback comes from an asynchronous I/O process?

Thanks.


原文:https://stackoverflow.com/questions/9896980
更新时间:2023-08-23 07:08

最满意答案

通常可以将复杂的update转换为merge

merge into proc_try k
using
   ( select jobs
     from   ( with a(namehost) as
                   ( select b.namehost
                     from   definition   a
                            join schema.nodes b
                                 on  b.namehost = a.idnode
                                 or  (b.nodeid = a.idnode and nodetype <> 'R')
                     group  by b.namehost
                     having sum(1 + lengthb(namejob)) - 1 > 4000 )
              select namehost
                   , 'TOOLONG' as jobs
              from   a
              union all
              select p.namehost
                   , listagg(p.namejob, ',') within group(order by p.namejob) as jobs
              from   ( select distinct
                              b.namejob, a.namehost
                       from   schema.nodes a
                              left join definition b
                                   on  b.idnode in (a.nodeid, a.namehost)
                                   and b.application not like '@NOTINCLUDE'
                       where  a.namehost not in (select * from a)
                       and    nodetype not like 'R'
                     ) p
              group  by p.namehost
            ) random
   ) new_jobs
on (k.namehost = new_jobs.namehost)
when matched then update set k.detail = new_jobs.jobs;

这是未经测试的,因为我没有您的表格或样本数据。

编辑:看起来我们可以简化一下,对此:

merge into proc_try k
using
   ( with overlength (namehost) as
          ( select n.namehost
            from   definition d
                   join schema.nodes n
                        on  n.namehost = d.idnode
                        or  (n.nodeid = d.idnode and nodetype <> 'R')
            group by n.namehost
            having sum(1 + lengthb(n.namejob)) - 1 > 4000 )
     select o.namehost, 'TOOLONG' as jobs
     from   overlength o
     union all
     select sd.namehost
          , listagg(sd.namejob, ',') within group(order by sd.namejob) as jobs
     from   ( select distinct d.namejob, n.namehost
              from   schema.nodes n
                     left join definition d
                          on  d.idnode in (n.nodeid, n.namehost)
                          and d.application not like '@NOTINCLUDE'
              where  n.namehost not in (select o.namehost from overlength o)
              and    n.nodetype not like 'R'
            ) sd
     group  by sd.namehost
   ) new_jobs
on (new_jobs.namehost = k.namehost)
when matched then update set k.detail = new_jobs.jobs;

我仍然看不到什么

sum(1 + lengthb(namejob)) - 1

尽管如此,是为了做。 看起来这可以简化为

sum(lengthb(namejob))

You can generally convert a complex update into a merge:

merge into proc_try k
using
   ( select jobs
     from   ( with a(namehost) as
                   ( select b.namehost
                     from   definition   a
                            join schema.nodes b
                                 on  b.namehost = a.idnode
                                 or  (b.nodeid = a.idnode and nodetype <> 'R')
                     group  by b.namehost
                     having sum(1 + lengthb(namejob)) - 1 > 4000 )
              select namehost
                   , 'TOOLONG' as jobs
              from   a
              union all
              select p.namehost
                   , listagg(p.namejob, ',') within group(order by p.namejob) as jobs
              from   ( select distinct
                              b.namejob, a.namehost
                       from   schema.nodes a
                              left join definition b
                                   on  b.idnode in (a.nodeid, a.namehost)
                                   and b.application not like '@NOTINCLUDE'
                       where  a.namehost not in (select * from a)
                       and    nodetype not like 'R'
                     ) p
              group  by p.namehost
            ) random
   ) new_jobs
on (k.namehost = new_jobs.namehost)
when matched then update set k.detail = new_jobs.jobs;

This is untested as I don't have your tables or sample data.

Edit: Looks like we can simplify it a bit, to this:

merge into proc_try k
using
   ( with overlength (namehost) as
          ( select n.namehost
            from   definition d
                   join schema.nodes n
                        on  n.namehost = d.idnode
                        or  (n.nodeid = d.idnode and nodetype <> 'R')
            group by n.namehost
            having sum(1 + lengthb(n.namejob)) - 1 > 4000 )
     select o.namehost, 'TOOLONG' as jobs
     from   overlength o
     union all
     select sd.namehost
          , listagg(sd.namejob, ',') within group(order by sd.namejob) as jobs
     from   ( select distinct d.namejob, n.namehost
              from   schema.nodes n
                     left join definition d
                          on  d.idnode in (n.nodeid, n.namehost)
                          and d.application not like '@NOTINCLUDE'
              where  n.namehost not in (select o.namehost from overlength o)
              and    n.nodetype not like 'R'
            ) sd
     group  by sd.namehost
   ) new_jobs
on (new_jobs.namehost = k.namehost)
when matched then update set k.detail = new_jobs.jobs;

I still can't see what

sum(1 + lengthb(namejob)) - 1

is meant to do, though. It looks like that could be simplified to

sum(lengthb(namejob))

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。