2

I'm trying to solve problem where I'm doing a math computation on main thread and I want to move it into child process. The problem is that I can't come up with an simple solution how to do that.

Here's what I have already:

  1. I investigated child_process.exec
  2. Managed to pass and return simple data to and from the process (like Fibonacci seq)

Problem that I can not solve:

  1. My calculation uses array of objects (like: [{x: 1, y: 2, z: 3}, ...])
  2. child_process.exec can only pass command line arguments
  3. I can not figure out how can I pass complex data structures to child process?

Maybe there is a library that can abstract all this logic and I could do something like:

doHeaveComputation(function (result) { 
  //do something with result
});

Thank you for help

Vytautas Butkus
  • 5,365
  • 6
  • 30
  • 45

1 Answers1

1

Try child_process.fork(). It returns ChildProcess object with send method, which allows to send serializable data and sockets to child process. Also you can receive messages from child processes with message event.

More in docs: http://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle

vkurchatkin
  • 13,364
  • 2
  • 47
  • 55