[英]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.