Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to implement CPU bound tasks in nodejs

I am using nodejs for a web server which decodes the GET params and returns data in some encoded format. The decode/encode are done using the crypto module of nodejs, which seems to be synchronous. While the time taken to serve a single request is fast enough, blocking the event loop has made the service perform poorly with concurrency.

My requirement is simple, make the encode/decode functionality outside of the event loop.

  1. Separate Process (child_process or cluster)

This can either be a separate process solely for this purpose, but since the encode/decode will be blocking in the child process this will stop the child process to receive new messages i.e. there will never be a situation when two strings are encoded as child process will also be single threaded.

  1. Separate Thread for each request (threads-a-gogo or fiber or node-webworker)

Create a separate thread for each request to carry out the encode/decode operation, but none of the modules seems to work as expected, i.e threads-a-gogo doesn't install through npm, fiber didn't create a separate thread on run(), node-webworker not working.

Has someone faced a similar problem or is there some way to easily create threads in nodejs with simple message passing.

like image 511
mdprasadeng Avatar asked May 30 '12 11:05

mdprasadeng


People also ask

How do you handle CPU intensive tasks in NodeJS?

Since Node. js is single-threaded and non-blocking, you can achieve higher concurrency with the same resources". And when you read about what it's bad at it usually goes like this: "Since Node. js is single-threaded, CPU-intensive tasks will block all requests from completing, until the task is completed.

Can NodeJS be used for CPU intensive applications?

Node. js provides developers a system with single-threaded event loop architecture that provides a non-blocking I/O mechanism. This works great until we get to CPU-intensive tasks.

What are CPU-bound tasks?

In computer science, a computer is CPU-bound (or compute-bound) when the time for it to complete a task is determined principally by the speed of the central processor: processor utilization is high, perhaps at 100% usage for many seconds or minutes.

What are some CPU intensive applications?

Sorting, search, graph traversal, matrix multiply are all CPU operations, a process is CPU-intensive or not it depends on how much and how frequent are their execution.


1 Answers

This is built into node's child processes. Docs here:

http://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle

You could also use cluster:

http://nodejs.org/api/cluster.html#cluster_cluster_fork_env

With cluster, it would work something like:

if (cluster.isMaster) {
  var worker = cluster.fork();
  worker.send('encodeThisString');

} else if (cluster.isWorker) {
  process.on('message', function(msg) {
    var encrypted = crypto.doSomeEncryption(msg);
    process.send(encrypted);
  });
}
like image 183
hunterloftis Avatar answered Oct 02 '22 16:10

hunterloftis