是否有可能以某种方式在 NodeJS 中进行多线程? [英] Is it possible somehow do multithreading in NodeJS?

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

问题描述

所以我有一个带有 Socket.IO 的应用程序,目的是在不同的站点上搜索一些数据.像爬虫之类的东西......主要问题是搜索过程太长,而它发生时我的应用程序卡住了......例如,如果一个用户开始第二次搜索需要等到第一次完成......

So i'm have an app with Socket.IO which purpose is to search some data on different sites. Something like crawler... The main problem is that the search process is too long and while it happens my app stucks... For example if one user starts to search second need to wait until first completed...

需要搜索的每个站点都表示为一个单独的类,因此我执行以下操作:

Each site which need to be searched is represented as a separate class so i do something like:

selected_sites.forEach(function(site_name) {
    var site = new sites[site_name];

    site.on('found', function(data) {
        socket.emit('found', data);
    });

    site.on('not_found', function() {
        socket.emit('not_found', 'Nothing found at ' + site.getSiteName());
    });

    site.search(socket_data.params);
});

是否有可能以某种方式将类主体|搜索进度"移动到其他地方|在新线程中",以便在搜索过程中不会阻塞事件循环?

Is it possible somehow to move the "class body | search progress" "somewhere else | in a new thread" so that event loop not be blocked while search in progress?

推荐答案

node.js 不允许您同时运行多个 Javascript 执行线程.单个 node.js 进程一次仅运行一个 Javascript 执行线程.由于异步 I/O,多个 Javascript 操作可能会进行中"在任何给定时间,但只有一个在任何给定时间实际运行(而其他可能正在等待 I/O 操作完成).

node.js does not allow you to run more threads of Javascript execution at the same time. A single node.js process only runs one Javascript thread of execution at a time. Because of asynchronous I/O, multiple Javascript operations may be "in flight" at any given time, but only one is actually running at any given time (while the others may be waiting for I/O operations to complete).

解决问题的常用方法是将一些耗时的操作和/或 CPU 密集型应用程序在后台运行,而您的服务器可以自由处理传入的请求,这是将耗时的操作移到它自己的 node.js 中进程(通常使用子进程模块),然后允许这两个进程根据需要共享信息,通过数据库或通过一些进程间通信,如套接字.

The usual way to solve a problem where you want some longer running and/or CPU intensive application to be run in the background while your server is free to handle incoming requests is to move the time consuming operation into it's own node.js process (often using the child process module) and then allow those two processes to share information as required, either via a database or via some interprocess communication like sockets.

如果您有多个 CPU 密集型操作,您可以启动多个辅助进程,或者您可以使用 节点.js 集群模块,以最大限度地利用主机中的所有 CPU.

If you have multiple CPU intensive operations, you can fire up multiple secondary processes or you can use the node.js clustering module in order to take maximum advantage of all CPUs in the host computer.

您应该知道,如果您的大部分代码只是网络或文件 I/O,那么这一切都可以通过异步操作来完成,并且您的 node.js 服务器将可以很好地扩展以并行处理许多不同的事情.如果您有 CPU 密集型操作(大量解析或计算),那么您将需要启动多个进程以更有效地利用多个 CPU 并让系统为您分配时间.

You should know that if most of your code is just networking or file I/O, then that can all be done with asynchronous operations and your node.js server will scale quite well to doing many different things in parallel. If you have CPU intensive operations (lots of parsing or calculations), then you will want to start up multiple processes in order to more effectively utilize multiple CPUs and let the system time slice the work for you.

2020 年更新:Nodejs 现在有线程.您可以使用 工作线程.这不需要并行化 I/O 操作,但对于并行化 CPU 密集型操作和利用多个 CPU 内核可能很有用.

Update in 2020: Nodejs now has threading. You can use Worker Threads. This would not be needed to parallelize I/O operations, but could be useful for paralellizing CPU-heavy operations and taking advantage of multiple CPU cores.

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

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