如何为具有绑定IO绑定和CPU绑定任务的Node.js应用程序应用群集/散布子进程技术? [英] How to apply clustering/spawing child process techniques for Node.js application having bouth IO bound and CPU bound tasks?

查看:116
本文介绍了如何为具有绑定IO绑定和CPU绑定任务的Node.js应用程序应用群集/散布子进程技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个IOT项目,其中Node.js应用程序执行以下任务:

1.使用异步消息传递库(绑定IO)读取消息流
2.根据Node.js应用程序发送的消息将消息发送到发生机器学习的Web服务(绑定到I/O,因为仅涉及API调用)
3.从Web服务接收使用机器学习的结果生成的模式(使用REST API) 4.将模式与实时流消息进行比较(由于模式匹配涉及复杂的算法,因此需要占用大量CPU的资源).
5.记录堆栈跟踪(与IO绑定)

将开发一个node.js应用程序,以将这些功能作为默认情况下在单线程下运行的单独任务来实现.事实是,产生子进程仅对CPU密集型任务有用,因此如何为node.js进程进行集群会同时破坏IO和CPU绑定的任务?我们是否需要在此node.js应用程序上部分执行集群?

任何人都可以为该node.js应用程序建议有效的体系结构吗?

解决方案

如果您有任何占用大量CPU的任务,请对所有请求使用群集.

集群进程也在做一些I/O密集型工作这一事实并不会伤害您,但是您将希望集群进程用于CPU密集型工作.因此,只需将服务器集群化,让每个集群处理请求的全部负载(I/O和CPU东西)即可.

简而言之,CPU密集型内容是群集的主要驱动程序.如果集群进程也正在执行非阻塞I/O,则不会有任何伤害.实际上,在高负载情况下,最多群集可用的CPU甚至可以帮助I/O绑定进程(尽管对CPU密集型进程的帮助不多).


尽管可能是一个更复杂的实现,但另一种选择是仅将子进程或新的Worker线程用于请求处理的CPU密集型部分.在这种情况下,您将创建某种工作队列和一组子进程或Worker线程,以在队列中执行操作,而您的主进程会将任务分配给队列中的每个子进程.使用这种方案,尽管现在必须使用某种进程间通信在两者之间进行协调,但是您可以准确地决定哪些代码是通过工作队列执行的,哪些代码保留在主流程中.

I'm working on a IOT project where the Node.js application perform following tasks:

1. Reading stream of messages using asynchronous messaging library (IO bound)
2. Sending the messages to web service where machine learning happens based on the messages that were sent by Node.js application (IO bound as only API call is involved)
3. Receive the pattern generated as a result of machine learning from web service (using REST API) 4. Compare the pattern against the real-time streaming messages (CPU intensive as complex algorithms are involved for pattern matching).
5. Logging stack traces (IO bound)

A node.js application is going to be developed to have these functionalities as separate tasks running under a single-thread by default. Being the fact that, spawning the child process will be useful only for CPU intensive tasks, how to to do clustering for node.js process doint both IO and CPU bound tasks? Do we need to partially perform clustering on this node.js application?

Can anyone please suggest the effective architecture for this node.js application?

解决方案

If you have ANY CPU-intensive tasks, then use clustering for all requests.

The fact that a clustered process is also doing some I/O intensive stuff won't hurt you, but you will want the clustered process for the CPU intensive stuff. So, just make your server clustered and let each cluster handle the whole load of a request (both the I/O and the CPU stuff).

In a nutshell, CPU-intensive stuff is the primary driver for clustering. It doesn't hurt anything if the clustered processes are also doing non-blocking I/O. In fact, clustering up to the number of CPUs available can even help I/O bound processes some too in high load situations (though not nearly as much help as with CPU-intensive processes).


An alternative, though it may be a more complicated implementation, is to use child processes or the new Worker threads only for the CPU-intensive parts of your request handling. In that case, you'd create some sort of work queue and a set of child processes or Worker threads for performing operations in the queue and your master process would distribute tasks to each child process from the queue. Using this scheme, you can decide exactly which code is executed via the work queue and which code stays in the main process, though you now have to coordinate between the two using some sort of interprocess communication.

这篇关于如何为具有绑定IO绑定和CPU绑定任务的Node.js应用程序应用群集/散布子进程技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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