Drejk
Drejk

Reputation: 115

Make time intensive function asynchronous

What's the best way to make a time intensive function, such as image manipulation, run asynchronously to allow other code to run or to allow multiple instances of said function run in parallel? (can be Node specific) For example like fs.readFile() or fetch() (XHR).

Upvotes: 3

Views: 368

Answers (1)

jfriend00
jfriend00

Reputation: 707158

Because node.js runs your Javascript as single threaded (only one piece of Javascript ever executing at a time), if your compute intensive image manipulation is currently all Javascript code, then your options for running your image manipulation in parallel with other Javascript are as follows:

  1. Fire up one or more worker processes (which can run node.js code or any other program you want) and do the image manipulation there in a separate process. They can then communicate back the result with any form of interprocess communication. This is probably the most common way to solve CPU intensive issues in node.js (offloading the CPU intensive stuff to other processes). You can use the child_process module to start these other processes and to communicate with them.

  2. Write a native code add-on for node.js that uses native threads and native code to do the image manipulation and that offers an asynchronous interface to node.js code. You can then call that interface asynchronously from node.js and be notified when it is complete, but node.js will be free to work on other things while it is working.

  3. Break the image manipulation into very small work chunks such that you can execute a small chunk of work (like a few ms of work), schedule the next chunk of work to run in a few ms and return control back to the event sub-system so it can process other events interleaved with your small chunks of work. FYI, design complicated code to run in small chunks is hard. You end up having to essentially build a state machine that can do a small amount of work and return, only to be called later to do some more work.

Upvotes: 4

Related Questions