是多线程的node.js版本? [英] A version of node.js that is multi-threaded?

查看:121
本文介绍了是多线程的node.js版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

node.js是单线程的并且遵循事件模型是很棒的,但是除此之外,它对我没有多大帮助.我有很多CPU绑定的任务,我希望每个服务器请求都在单独的线程中处理.有没有人创建过多线程的node.js分支?

It's great that node.js is single threaded and follows an event model, but besides that it doesn't help me much. I have alot of cpu-bound tasks and I want each server request to be handled in a separate thread. Has anyone created a node.js fork that is multi-threaded yet?

我的意思不是像 webworker-threads 这样的模块,我需要在其中包装我的每个功能(在大型项目中都很麻烦).我的意思是,每个请求都像大多数其他Web服务器一样作为单独的线程进入哪里?

I don't mean a module like webworker-threads where I need to wrap each one of my functions (which gets cumbersome for large projects). I mean, where each request comes in as a separate thread like most other web servers?

推荐答案

常见的解决方案是使用

The common solution is to spawn several node.js threads (up to the number of CPU cores) using build-in cluster module or some third-party library.

在单独的线程中处理每个请求违背了node.js的理念,并使所有node.js的优势无效.使用node.js来完成这些任务毫无意义,因为还有很多其他Web服务器会做得更好.

Handling each request in a separate thread goes against node.js philosophy and nullifies all node.js advantages. There is no point in using node.js for such tasks, because there is a lot of other web servers which will do it much better.

另一方面,node.js为您提供了一种处理Web请求的更有效的方法.因此,node.js不会为每个请求生成一个新线程,而是使用100%的单个CPU内核来处理单个线程(或小的线程池)中的所有请求.

On the other side, node.js offers you a more efficient way of handling web requests. So, instead of spawning a new thread for each request, node.js handles all requests in a single thread (or in a small pool of threads) using 100% of a single CPU core.

您还应该记住,node.js是为处理I/O绑定任务而设计的,可以击败其中的任何其他Web服务器.但是node.js不太擅长处理CPU限制的任务(例如计算).因此,如果处理每个请求都需要进行大量计算,那么您选择了错误的工具.

You should also keep in mind that node.js was designed for handling I/O bound tasks and can beat any other web server in it. But node.js is not so good at handling CPU bound tasks (e.g. calculations). So, if handling each request requires a lot of calculations then you picked the wrong tool.

这篇关于是多线程的node.js版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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