简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM