为什么node.js一次只处理六个请求?(Why is node.js only processing six requests at a time?)
我们有一个node.js服务器,它实现一个REST API作为中央服务器的代理,它具有稍微不同的,不幸的是不对称的REST API。
我们的客户端在各种浏览器中运行,要求节点服务器从中央服务器获取任务。 节点服务器从中央获取所有任务ID的列表,并将其返回给客户端。 客户端然后通过代理对每个id进行两次REST API调用。
据我所知,这些东西都是异步完成的。 在控制台日志中,当我启动客户端时,它看起来像这样:
Requested GET URL under /api/v1/tasks/*: /api/v1/tasks/
这需要几秒钟的时间才能从中央服务器获取列表。 一旦得到响应,服务器barfs这样很快:
Requested GET URL under /api/v1/tasks/id/:id :/api/v1/tasks/id/438 Requested GET URL under /api/v1/workflow/id/:id :/api/v1/workflow/id/438 Requested GET URL under /api/v1/tasks/id/:id :/api/v1/tasks/id/439 Requested GET URL under /api/v1/workflow/id/:id :/api/v1/workflow/id/439 Requested GET URL under /api/v1/tasks/id/:id :/api/v1/tasks/id/441 Requested GET URL under /api/v1/workflow/id/:id :/api/v1/workflow/id/441
然后,每次这些请求都从中央服务器得到一个结果,那么另外两行就会非常快速地被删除。
所以似乎我们的node.js服务器只愿意一次有六个请求。
We have a node.js server which implements a REST API as a proxy to a central server which has a slightly different, and unfortunately asymmetric REST API.
Our client, which runs in various browsers, asks the node server to get the tasks from the central server. The node server gets a list of all the task ids from the central one and returns them to the client. The client then makes two REST API calls per id through the proxy.
As far as I can tell, this stuff is all done asynchronously. In the console log, it looks like this when I start the client:
Requested GET URL under /api/v1/tasks/*: /api/v1/tasks/
This takes a couple seconds to get the list from the central server. As soon as it gets the response, the server barfs this out very quickly:
Requested GET URL under /api/v1/tasks/id/:id :/api/v1/tasks/id/438 Requested GET URL under /api/v1/workflow/id/:id :/api/v1/workflow/id/438 Requested GET URL under /api/v1/tasks/id/:id :/api/v1/tasks/id/439 Requested GET URL under /api/v1/workflow/id/:id :/api/v1/workflow/id/439 Requested GET URL under /api/v1/tasks/id/:id :/api/v1/tasks/id/441 Requested GET URL under /api/v1/workflow/id/:id :/api/v1/workflow/id/441
Then, each time a pair of these requests gets a result from the central server, another two lines is barfed out very quickly.
So it seems our node.js server is only willing to have six requests out at a time.
原文:https://stackoverflow.com/questions/12060869
最满意答案
如果您将xargs安装在多核处理器上,您可以从以下情况中受益,以防有人感兴趣。
环境:
Processor: Dual Quad-core 2.4GHz Memory: 32 GB Number of files: 584450 Total Size: ~ 35 GB
测试:
1.找到必要的文件,将它们管理到xargs并告诉它执行8个实例。
time find ./ -name "*.ext" -print0 | xargs -0 -n1 -P8 grep -H "string" >> Strings_find8 real 3m24.358s user 1m27.654s sys 9m40.316s
2.找到必要的文件,将它们管理到xargs,并告诉它执行4个实例。
time find ./ -name "*.ext" -print0 | xargs -0 -n1 -P4 grep -H "string" >> Strings real 16m3.051s user 0m56.012s sys 8m42.540s
3. @Stephen建议:找到必要的文件,并使用+而不是xargs
time find ./ -name "*.ext" -exec grep -H "string" {} \+ >> Strings real 53m45.438s user 0m5.829s sys 0m40.778s
定期递归grep。
grep -R "string" >> Strings real 235m12.823s user 38m57.763s sys 38m8.301s
为了我的目的,第一个命令工作得很好。
If you have xargs installed on a multi-core processor, you can benefit from the following just in case someone is interested.
Environment:
Processor: Dual Quad-core 2.4GHz Memory: 32 GB Number of files: 584450 Total Size: ~ 35 GB
Tests:
1. Find the necessary files, pipe them to xargs and tell it to execute 8 instances.
time find ./ -name "*.ext" -print0 | xargs -0 -n1 -P8 grep -H "string" >> Strings_find8 real 3m24.358s user 1m27.654s sys 9m40.316s
2. Find the necessary files, pipe them to xargs and tell it to execute 4 instances.
time find ./ -name "*.ext" -print0 | xargs -0 -n1 -P4 grep -H "string" >> Strings real 16m3.051s user 0m56.012s sys 8m42.540s
3. Suggested by @Stephen: Find the necessary files and use + instead of xargs
time find ./ -name "*.ext" -exec grep -H "string" {} \+ >> Strings real 53m45.438s user 0m5.829s sys 0m40.778s
4. Regular recursive grep.
grep -R "string" >> Strings real 235m12.823s user 38m57.763s sys 38m8.301s
For my purposes, the first command worked just fine.
相关问答
更多-
如何有效地使用grep?(How to use grep efficiently?)[2024-01-24]
如果您将xargs安装在多核处理器上,您可以从以下情况中受益,以防有人感兴趣。 环境: Processor: Dual Quad-core 2.4GHz Memory: 32 GB Number of files: 584450 Total Size: ~ 35 GB 测试: 1.找到必要的文件,将它们管理到xargs并告诉它执行8个实例。 time find ./ -name "*.ext" -print0 | xargs -0 -n1 -P8 grep -H "string" >> Strings_f ... -
如何对整个字进行grep(How to grep for the whole word)[2023-11-03]
你想要-w选项来指定它是单词的结尾。 find . | xargs grep -sw 's:text' You want the -w option to specify that it's the end of a word. find . | xargs grep -sw 's:text' -
用grep转义<和>(Escaping < and > with grep)[2022-05-26]
转义它们会激活它们的元字符属性并将它们转换为GNU grep中的单词边界: $ grep 'foo' file foofoobar $ grep '\ ' file foo 上面的第二个grep没有查找字符串 ,它正在寻找字符串foo不会在字构成字符之前或之后立即成功。 一般来说,如果不确切知道这意味着什么,就逃脱角色是不安全的。 这是另一个例子: $ printf 'aa\na{2}b\n' aa a{2}b $ printf 'aa\na{2}b\n' ... -
在grep中使用OR(Using OR in grep)[2021-04-29]
grep -B3 'Max_value: \(127\|32767\)' proc_*.* grep -B3 'Max_value: \(127\|32767\)' proc_*.* -
用通配符grep(grep with wildcards)[2022-12-21]
如果这些是您需要搜索的唯一字符串,请使用-F (grep表示固定字符串): grep -F "directory1 directory2 directory3" file.txt 如果你想使用更高级的正则表达式grep,请使用-E (使用扩展正则表达式): grep -E 'directory[1-3]' file.txt 请注意,某些grep (如GNU grep)不需要-E来运行此示例。 最后,请注意您需要引用正则表达式。 如果不这样做,你的shell可能会首先根据路径名扩展扩展正则表达式(例如,如 ... -
在stdin上使用grep(Use grep on stdin)[2023-04-08]
这似乎很好。 也许你可以在dup2之后关闭一些fds: // in child process dup2(fd[0], STDIN_FILENO); close(fd[0]); close(fd[1]); 编辑: 似乎有拼写错误: "/bin/grep/" 它应该是: "/bin/grep" It seems to be quite OK. Maybe you could close some fds after dup2: // in child process dup2(fd[0], STDIN_FI ... -
如何通过awk使用grep?(How to use grep via awk?)[2021-11-23]
在Gnu Awk中,您可以使用\<和\>来匹配单词的开头和结尾,所以 gawk '/\/{++i} END{print i}' 会做同样的事情 grep -wc 'GOOD' file 如果你想计算GOOD这个词的出现总数(不仅是行数,还有给定行/记录中的出现次数),你可以在Gnu Awk版本4中使用FPAT , gawk 'BEGIN { FPAT="\\ "; RS="^$" } { print NF }' file 如果要计算给定记录中短语GOOD DI的完全匹配数, ... -
使用数学和grep(Using math and grep)[2023-09-24]
grep有-c选项只打印事件,你应该利用它而不是产生另一个进程和一个匿名管道: grep -c '' file.txt 要从两次搜索中减去计数,您可以使用命令替换直接减去它们: echo $(( $(grep -c ' ' file.txt) - $(grep -c ' ' file.txt) )) 如果您喜欢,也可以使用两个变量: count_1=$(grep -c ' ' file.txt) count_2=$(grep ... -
? 不是grep支持的基本正则表达式的一部分。 GNU grep支持它们作为扩展,但你必须逃避它们: $ grep '^\[\?MyTag\]\?' file.txt [MyTag] hello MyTag world 或者,正如指出的那样,使用grep -E来启用扩展正则表达式。 对于GNU grep, grep和grep -E之间的唯一区别,即使用基本和扩展正则表达式,是你必须逃避的,不是什么。 基本正则表达式 必须转义捕获组和量化: \( \)和\{ \} 零或一( ? ),一个或多个( + )和交 ...
-
在grep中使用“^ - ”(Usage of “^-” in grep)[2022-06-14]
在${CONFIG_FILE}之后,我没有看到x任何目的。 也许有一个早期版本的脚本,他们没有引用变量,所以他们写道 echo -e ${CONFIG_FILE}x 在这种情况下,即使变量为空, x确保有一个echo参数。 但引号解决了这个问题,因此不再需要x 。 grep -q "^-"测试输入的第一个字母是否为- 。 正则表达式^匹配行的开头,并且-匹配自身。 -q选项告诉grep不打印匹配行,它只是根据它是否匹配来设置其退出状态。 I don't see any purpose to the x a ...