简体   繁体   English

在nodejs中运行单独线程的最佳方法是什么?

[英]What is the best way to run separate thread in nodejs?

I have a react application, and I need a thread/process outside of the main thread to run some functions every hour, or even every day.我有一个反应应用程序,我需要一个主线程之外的线程/进程每小时甚至每天运行一些函数。

I don't know how to implement this, whether create a new API using Java or other app, or use js' setInterval() technique.我不知道如何实现这一点,是使用 Java 或其他应用程序创建新 API,还是使用 js 的 setInterval() 技术。 I found nodejs Worker Threads too.我也发现了 nodejs Worker Threads。 But I don't know what is the best way to implement it.但我不知道实现它的最佳方法是什么。

The process itself is getting a data (an array) from public API, and process the data periodically, it will be a 'stay online' process since I want to run this in a cloud and stay online for a long time.该过程本身正在从公共 API 获取数据(数组),并定期处理数据,这将是一个“保持在线”过程,因为我想在云中运行它并长时间保持在线状态。

So, can I have any suggestion of how to implement this?那么,我可以对如何实现这一点提出任何建议吗?

Thank you very much.非常感谢。

You can use this lib Node Schedule您可以使用此 lib节点计划

or you can use node.js built in lib child_process或者你可以使用内置在 lib child_process 中的node.js

spawn(), exec(), execFile(), and fork() spawn()、exec()、execFile() 和 fork()

If you want to use thread then latest version of node.js has support for worker_threads .如果你想使用线程,那么最新版本的 node.js 支持worker_threads If you want to use a separate process then child_process is what you are looking for.如果您想使用单独的进程,那么child_process就是您要寻找的。

Here is an article about how to parse json using worker_thread .这是一篇关于如何使用 worker_thread 解析 json 的文章 Using same method you can do any CPU intensive task in a separate thread without blocking the main thread.使用相同的方法,您可以在单独的线程中执行任何 CPU 密集型任务,而不会阻塞主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在加载,调整大小和滚动时-运行单独功能的最佳方法是什么? - On Load, On Resize and On Scroll - what's the best way to run separate functions? 将 React 连接到 Nodejs 后端的最佳方式是什么 - What is best way to connect React to Nodejs backend 在angularjs中运行jquery代码的最佳方法是什么? - what is the best way to run jquery code in angularjs? 在Java中运行JavaScript的最佳方法是什么? - What is the best way to run JavaScript in Java? 在 MongoDB 和 nodejs 上运行 1k+ 查询的最佳方法? - Best way to run 1k+ queries on MongoDB and nodejs? 从两个单独的JavaScript对象合并属性的最佳方法是什么? - What is the best way to merge properties from two separate JavaScript objects? 在 react-router-dom 中分离路由的最佳方法是什么? - What's the best way to separate the routes in react-router-dom? 在laravel处理两个独立的ajaxes的最佳方法是什么? - What is the best way to handle two separate ajaxes in laravel? 在NodeJS和Mocha中循环循环的最佳方法是什么? - What's the best way to loop inside a loop in NodeJS and Mocha? 对 Nodejs 中发出的事件进行单元测试的最佳方法是什么? - What's the best way to unit test an event being emitted in Nodejs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM