rick
rick

Reputation: 426

Executing potentially expensive tasks

I have a nodejs tcp server that handles the state of many child servers, when something happens the state server tells a child server to perform an action, it can be potentially expensive, most cases it will be quite expensive (around a minute), on some occasions it will be done in a couple of seconds.

Due to what the child servers are doing (listening on websockets and passing quite a lot of data back and forth very frequently), I can't afford to have the child server stall while operating this expensive task.

Obviously nodejs doesn't have the ability to spawn new threads, without some messy implementation of them. Would it be worth spawning a new process to execute this potentially expensive task, I can determine when it is going to be expensive by the command sent from the parent server.

Upvotes: 0

Views: 275

Answers (1)

Timothy Strimple
Timothy Strimple

Reputation: 23070

Node.js does have the ability to spawn other processes, and it handles them quite well. Check out child_processes in the node.js documentation, and specifically the fork command which allows you to spawn other node.js jobs and communicate with them via message channel.

Node.js Child Processes Node.js Fork

http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork

Upvotes: 1

Related Questions