最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the best way to run separate thread in nodejs? - Stack Overflow

programmeradmin2浏览0评论

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. I found nodejs Worker Threads too. 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.

So, can I have any suggestion of how to implement this?

Thank you very much.

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. I found nodejs Worker Threads too. 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.

So, can I have any suggestion of how to implement this?

Thank you very much.

Share asked Dec 6, 2019 at 10:59 Evan GunawanEvan Gunawan 4291 gold badge6 silver badges18 bronze badges 1
  • 1 if your server supports cron and crontab, adding your script there (node /path/to/script.js) would be the simplest solution. – georg Commented Dec 6, 2019 at 11:14
Add a ment  | 

2 Answers 2

Reset to default 4

You can use this lib Node Schedule

or you can use node.js built in lib child_process

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

If you want to use thread then latest version of node.js has support for worker_threads. If you want to use a separate process then child_process is what you are looking for.

Here is an article about how to parse json using worker_thread. Using same method you can do any CPU intensive task in a separate thread without blocking the main thread.

发布评论

评论列表(0)

  1. 暂无评论