如何创建线程的NodeJS [英] How to create threads in nodejs

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

问题描述

有没有办法为同时运行多种方法创建线程。如果有任何方法失败之间的所有其他线程应该被杀死。

在此先感谢

Is there any way to create threads for running multiple methods at a time. If any method fails in between all the other threads should be killed.
Thanks in advance

推荐答案

每一个的node.js过程是一个由设计线程。因此,为了得到多线程,你必须有多个进程(如一些其他海报指出,也有库,你可以链接到,这将使你在节点线程的工作能力,但不存在这样的能力,如果没有这些库见肖恩文森特回答引用<一个href=\"https://github.com/audreyt/node-webworker-threads\">https://github.com/audreyt/node-webworker-threads)

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)

您可以从您的主进程启动子进程如下所示Node.js的文件中: http://nodejs.org/ API / child_process.html 。这些例子pretty此页面上的好,是pretty直线前进。

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.

另请参阅: Node.js的多核机器

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

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