简体   繁体   English

要child_process分叉还是不分叉I / O任务?

[英]To child_process fork or not to fork for I/O tasks?

Does it make sense to use child_process fork for long running (15 - 30 seconds) I/O tasks such as fetching a feed and saving it to a db? 使用child_process fork进行长时间运行(15-30秒)的I / O任务(例如获取提要并将其保存到db)是否有意义?

The context for this question is for an express route, and I need to mention that a status response is sent to the browser early when the feed url has been validated. 这个问题的上下文是一条明确的路线,我需要提到的是,在验证Feed网址后,状态响应会早日发送到浏览器。 After the status response has been sent the fetching and saving of the feed items continues and can obviously take a bit of time (10-30 sec). 发送状态响应后,继续提取和保存Feed项,这显然会花费一些时间(10-30秒)。 Should this second part be forked with a child process? 第二部分是否应该与子进程分叉?

I have read contradictory posts (not on SO) about the I/O efficiency of node with/out forking the job to a background process, so I wanted to have a clear response to this. 我已经阅读了有关节点的I / O效率的矛盾文章(而不是关于SO的文章),该节点有/没有将工作分派给后台进程,因此我想对此有一个明确的回应。 Does it make sense to fork I/O tasks (not CPU intensive tasks per se which I reckon is a separate question) 分叉I / O任务是否有意义(我认为这本身不是CPU密集型任务,这是一个单独的问题)

In general cases, Node is great for handling I/O. 通常,Node非常适合处理I / O。 Due to the Event driven architecture, as soon as an I/O intensive action leaves Node (or any I/O action, really), Node forgets about that action until said action is finished (or has an error). 由于事件驱动的体系结构,一旦I / O密集型动作离开了Node(或者实际上是任何I / O动作),Node就会忘记该动作,直到该动作完成(或有错误)。 The returning Event then goes back into the single-threaded Node process. 然后,返回的事件返回到单线程Node进程。

Take for example a remote DB and an intensive query. 例如,一个远程数据库和一个密集查询。 Even if the DB server takes seconds to query and return the results, the Node process was only responsible for building the query (a string?), and putting said query on TCP socket. 即使DB服务器花费几秒钟查询并返回结果,Node进程也仅负责构建查询(字符串?),并将该查询放在TCP套接字上。 The transferring of data on the socket doesn't even take up the Node process! 在套接字上传输数据甚至不占用Node进程! Then, Node cares nothing about the request until the returning data has finished coming across the socket. 然后,Node不关心请求,直到返回的数据通过套接字完成为止。 (There could be some processing you don't see in your DB package, like when a RDBMS result is converted into JSON). (可能有一些处理未在数据库包中看到,例如将RDBMS结果转换为JSON时)。

There might be corner cases to this and those you will have to look out for . 可能会有一些极端的情况,您将需要注意的情况。 . . if they ever come up. 如果他们出现了。 A huge majority of the time, Node will handle I/O very well. 在绝大多数情况下,Node可以很好地处理I / O。 (Post some links to said articles, in your question or as comments under this answer.) (在您的问题中或在此答案下的评论中,张贴一些指向上述文章的链接。)

Forking child processes is typically reserved for high CPU tasks that would slow down the main event loop. 分叉子进程通常保留给高CPU任务使用,这会减慢主事件循环的速度。 There could be other reasons, but "in general." 可能还有其他原因,但“一般”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 守护被'fork'的'child_process' - Daemonize 'child_process' that was 'fork'ed Child_process fork-间歇性读错误 - Child_process fork - intermittent onread error 如何从Visual Studio代码中调试child_process fork过程 - How can I debug a child_process fork process from visual studio code React 应用程序中的 require('child_process').fork 方法“不存在” - require('child_process').fork method “does not exist” in React app 来自child_process和主要master的Node.js分支 - Nodejs fork from child_process and main master 为什么我在nodejs中child_process的fork方法的相同代码的不同设备中得到不同的输出? - why I'm getting different outputs in different devices of the same code of the child_process's fork method in nodejs? 如何使用茉莉花来模拟require('child_process')。fork(__ dirname +'/ worker') - How to mock require('child_process').fork(__dirname + '/worker') using jasmine 在IntelliJ IDEA上调试node.js child_process fork示例 - debugging node.js child_process fork example on IntelliJ IDEA 使用 node-dev 和 Typescript 忽略 child_process fork 的文件扩展名 - Ignore filename extension for child_process fork using node-dev and Typescript 不能在电子child_process分支中要求使用非本地模块 - Cannot require non-native module in electron child_process fork
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM