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);
- 将内容复制到文件中。
- 执行文件。
- 在浏览器中打开一个新选项卡。 将url设置为
localhost/1
。 不要走了。- 在浏览器中打开一个新选项卡。 将url设置为
localhost/2
。 不要走了。- 返回第一个标签。 按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);
- Copy the content into a file.
- Execute the file.
- Open a new tab in browser. Set the url to
localhost/1
. Don't go yet.- Open a new tab in browser. Set the url to
localhost/2
. Don't go yet.- 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 doneTab 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
最满意答案
通常可以将复杂的
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 amerge
: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))
相关问答
更多-
在SQL中更新无行(Update No Rows in SQL)[2022-07-06]
@@ RowCount是您正在寻找的。 @@RowCount is what you are looking for. -
你有没有尝试过 : UPDATE exp_channel_data t1 INNER JOIN exp_channel_titles t2 ON t1.entry_id = t2.entry_id AND t2.channel_id = 12 SET t1.field_id_131 = t2.url_title Have you tried : UPDATE exp_channel_data t1 INNER JOIN exp_channel_titles t2 ON t1.entry_id = t ...
-
%匹配任意数量的字符,甚至零个字符。 当你LIKE "%child, 20's%"你会错过child,和20's之间的%字符。 所以,你需要添加额外的% : WHERE `candidates`.`availability` = "yes" AND `candidates`.`playing_age` LIKE "%child,%20's%" % Matches any number of characters, even zero characters. When you do LIKE "%chil ...
-
通常可以将复杂的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 ...
-
据推测,两个表中有多个具有相同id的行。 你需要的是一个更好的连接键。 row_number()为您提供了灵活性。 以下是代码结构的样子: with toupdate as ( select a.*, row_number() over (partition by id order by id) as seqnum from a ) update toupdate set cola = b.colb from a join (select ...
-
你没有确定表之间的任何连接条件 ,所以你会得到一个笛卡尔积。 尝试这样的事情: SELECT b.isbn, l.lend_no, s.first_name FROM dbo.books b INNER JOIN dbo.lending l ON l.Book_id = b.Book_id -- just guessing here INNER JOIN dbo.students s ON l.student_id = s.student_id -- just guessin ...
-
这应该工作: UPDATE `ps_product_lang` t1 join `ps_product_lang` t2 on t1.id_lang = t2.id_lang SET t1.description = t2.description WHERE t1.id_shop = 2 AND t2.id_shop = 1 This should work: UPDATE `ps_product_lang` t1 join `ps_product_lang` t2 on t1.id_lang = t ...
-
更新SQL表的行(Update rows of a SQL table)[2024-01-08]
试试这个..你没有执行sql语句 $sql = "SELECT number, status FROM summonerid"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $SummonerID = $row["number"]; $status = $row["status"] ... -
所选行的SQL更新(SQL Update for Selected rows)[2021-10-03]
您可以使用联接来允许使用表进行更新以及查询结果 update `clients_branches` JOIN ( select client_code, count(*) FROM `clients_branches` WHERE NULLIF(clients_branches.invoice_email, '') IS NULL group by client_code HAVING COUNT(*) = 1 ) t on t.cli ... -
SELECT语句报告单行是加入查询的结果。 但是你要更新3个表: p.old_username (and several others) e.date_modified (and others) u.email 因此有3个更新。 The SELECT statement is reporting that a single row is the result of the joined query. But you are updating 3 tables: p.old_username (and se ...