Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start new process on nodejs

I'm working with node.js and I want to when there's a request to a url like ./calculate start a new process to make these complex calculations, and I want that process to continue even if the script which called it has finished. Is it possible?

Thank you.

like image 849
Mario Avatar asked Aug 09 '12 03:08

Mario


2 Answers

You can use the natively provided child process facility: http://nodejs.org/api/child_process.html

And use the unix "nohup" command to keep the spawned process alive even if the parent process died.

like image 56
Roman Avatar answered Oct 05 '22 06:10

Roman


There are different approach to this.

You could use https://github.com/pgriess/node-webworker.

Or much better http://nodejs.org/docs/latest/api/cluster.html#cluster_cluster.

Those solutions are if you want to do a subprocess in Node, you could also simply spawn a new Node process and wait for the output (http://nodejs.org/api/all.html#all_child_process_spawn_command_args_options) but node-webworker is a wrapper around that solution and is a much cleaner.

like image 27
3on Avatar answered Oct 05 '22 05:10

3on