如何在nodejs中创建线程 [英] How to create threads in nodejs

查看:65
本文介绍了如何在nodejs中创建线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法创建线程来同时运行多个方法?

这样,如果任何方法在所有其他线程之间失败,都应该被杀死.

解决方案

每个 node.js 进程都是单线程设计的.因此,要获得多个线程,您必须有多个进程(正如其他一些海报指出的那样,您还可以链接到一些库,这将使您能够在 Node 中使用线程,但是如果没有这些库,就不存在这种功能. 参见 Shawn Vincent 参考 https://github.com/audreyt/node-webworker-threads)

您可以从主进程启动子进程,如 node.js 文档中所示:http://nodejs.org/api/child_process.html.此页面上的示例非常好,而且非常简单.

然后,您的父进程可以监视它启动的任何进程上的关闭事件,然后可以强制关闭您启动的其他进程,以实现您正在谈论的一劳永逸"策略类型.

另见:多核机器上的Node.js>

Is there any way to create threads for running multiple methods at a time?

That way, if any method fails in between all the other threads should be killed.

解决方案

Every node.js process is single threaded by design. Therefore to get multiple threads, you have to have multiple processes (As some other posters have pointed out, there are also libraries you can link to that will give you the ability to work with threads in Node, but no such capability exists without those libraries. See answer by Shawn Vincent referencing https://github.com/audreyt/node-webworker-threads)

You can start child processes from your main process as shown here in the node.js documentation: http://nodejs.org/api/child_process.html. The examples are pretty good on this page and are pretty straight forward.

Your parent process can then watch for the close event on any process it started and then could force close the other processes you started to achieve the type of one fail all stop strategy you are talking about.

Also see: Node.js on multi-core machines

这篇关于如何在nodejs中创建线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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