是否有可能在nodejs中实现多线程? [英] is it possible to achieve multithreading in nodejs?

查看:252
本文介绍了是否有可能在nodejs中实现多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nodejs多线程..

nodejs multithreading..

天气可以在nodejs中使用多头?如是。
在nodejs中使用多头的优点和缺点是什么?哪些模块可以在nodejs中实现多线程?我是nodejs的新手,我从许多博客中读到,说节点是
单线程。
我知道java多线程但我需要知道天气是否可以节点

Weather it is possible to use a multitheading in nodejs? if yes. what are the advantages and disadvantages of using multitheading in nodejs.Which are those module that can be achieve multithreading in nodejs? I am newbie to nodejs, i read from many blogs saying that node is single threaded. I know the java multithreading but i need to know weather it is possible in node or not.

推荐答案

是和否。让我们从头开始。为什么NodeJs是单线程的,这里解释为什么Node.js是单线程的?

Yes and No. Let's start from the beginning. Why is NodeJs single-threaded, is explained here Why is Node.js single threaded?

虽然Node.js本身是多线程的 - I / O和其他此类操作从线程池运行 - 由Node.js执行的JavaScript代码运行,所有实际目的,在一个线程中。这不是Node.js本身的限制,而是V8 JavaScript引擎和JavaScript实现的限制。

While Node.js itself is multithreaded -- I/O and other such operations run from a thread pool -- JavaScript code executed by Node.js runs, for all practical purposes, in a single thread. This isn't a limitation of Node.js itself, but of the V8 JavaScript engine and of JavaScript implementations generally.

Node.js包含用于群集多个节点的本机机制.js进程,每个进程在一个单独的核心上运行。但是,该集群机制不包括任何本机路由逻辑或工作者之间的共享状态。

Node.js includes a native mechanism for clustering multiple Node.js processes, where each process runs on a separate core. But that clustering mechanism doesn't include any native routing logic or shared state between workers.

一般而言,更明确的说法是,每个node.js进程都是单线程的。如果你想要多个线程,你也必须拥有多个进程。
例如,你可以使用子进程,这里有 http:// nodejs。 org / api / child_process.html 。而且只是为了您的信息,请查看本文,非常有启发性且写得很好,如果您想使用child_processes,可能会对您有所帮助 - https://blog.scottfrees.com/automating-ac-program-from-a-node-js-web-app

Generally and more clearly the statement is that, each node.js process is single threaded .if you want multiple threads, you have to have multiple processes as well. For instance,you can use child process for this, which is described here http://nodejs.org/api/child_process.html . And just for your info, check out also this article, is very instructive and well written, and possibly will help you, if you want to work with child_processes -- https://blog.scottfrees.com/automating-a-c-program-from-a-node-js-web-app

尽管有上述所有内容,但您可以使用C ++和本地nodejs C ++开发实现一种多线程。

Despite of all of the above, you can achieve a kind of multi-threading with C++ and native nodejs C++ development.

首先查看这些答案,可能他们会帮助你,

First of all check out these answers, probably they will help you,

如何在nodejs中创建线程

Node.js C ++插件:来自不同主题的多个回调

Node.j s C ++插件:线程

https://bravenewmethod.com/2011/03/30/callbacks-from-threaded-node-js-c-extension/

当然,您可以找到并利用许多提供多线程功能的节点插件: https://www.npmjs.com/search?q=thread

Of course you can find and leverage a lot of node plugins which are giving "multi"-threading capability: https://www.npmjs.com/search?q=thread

此外,您还可以查看 JXCore https://github.com/jxcore/jxcore
JXCore 是Node.js的分支,允许Node.js应用程序在同一进程中的多个线程上运行。所以很可能 JXCore 是一个解决方案。

In addition, you can check JXCore https://github.com/jxcore/jxcore JXCore is fork of Node.js and allows Node.js apps to run on multiple threads housed within the same process. So most probably JXCore is a solution for you.


使用多个的优点和缺点是什么?在Node.js中进行线程化?

"What are the advantages and disadvantages of using multi-threading in Node.js ?"

这取决于你想做什么。如果您正确地利用和使用Node.js源,并且您的多个 - 线程插件或进程或其他任何东西,不会破解或滥用来自V8或Node.js核心的任何内容,则没有任何缺点!

It depends of what you want to do. There are no disadvantages if you leverage and use Node.js sources correctly, and your "multi" - threaded plugins or processes or whatever, do not "hack" or misuse anything from the core of V8 or Node.js !

正如在每个答案中一样,正确的答案是使用正确的工具来完成工作。
当然,由于节点是按设计单线程的,因此您可以拥有更好的多线程方法。

As in every answer, the correct answer is "use the right tools for the job". Of course, since node is by design single-threaded, you can have better approaches for multithreading.

许多人使用的技术是用C ++,Java,Python等制作多线程应用程序,然后通过自动化和Node运行它。 js child_process(第三方应用程序与自动化异步运行,你有更好的性能(例如C ++应用程序),你可以在Node.js应用程序中发送输入和输出)。

A technique that a lot of people use, is to make their multi-threaded application in C++, Java, Python e.t.c and then, they run it via automation and Node.js child_process (third-party application runs asynchronously with automation, you have better performance (e.g C++ app), and you can send input and get output in and from your Node.js application).


缺点多线程Node.js

Disadvantages multi-threading Node.js

检查:https://softwareengineering.stackexchange.com/questions / 315454 /什么是制作多线程-javascript-runtime-implementat的缺点

Check this: https://softwareengineering.stackexchange.com/questions/315454/what-are-the-drawbacks-of-making-a-multi-threaded-javascript-runtime-implementat

请记住,如果你想通过修改它来在Node.js中创建一个纯多线程环境,我认为由于复杂性,这将是困难的,风险很大,而且你有对于每个新版本的V8或Node版本,它总是最新的,可能会影响到这一点。

Keep in mind that if you want to create a pure multithreaded environment in Node.js by modifying it, I suppose that would be difficult, risky due to the complexity, moreover you have to be, always up to date with each new V8 or Node release that will probably affect this.

希望这会有所帮助。

这篇关于是否有可能在nodejs中实现多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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