What's the most efficient way to prevent a node.js script from terminating?

If I'm writing something simple and want it to run until explicitly terminated, is there a best practice to prevent script termination without causing blocking, using CPU time or preventing callbacks from working?

I'm assuming at that point I'd need some kind of event loop implementation or a way to unblock the execution of events that come in from other async handlers (network io, message queues)?

A specific example might be something along the lines of "I want my node script to sleep until a job is available via Beanstalkd".


I think the relevant counter-question is "How are you checking for the exit condition?".

If you're polling a web service, then the underlying setInterval() for the poll will keep it alive until cancelled. If you're taking in input from a stream, that should keep it alive until the stream closes, etc.

Basically, you must be monitoring something in order to know whether or not you should exit. That monitoring should be the thing keeping the script alive.


Node.js end when it have nothing else to do.

If you listen on a port, it have something to do, and a way to receive beanstalk command, so it will wait.

Create a function that close the port and you ll have your explicit exit, but it will wait for all current job to end before closing.

链接地址: http://www.djcxy.com/p/52692.html

上一篇: 如何强制保持Node.js进程终止?

下一篇: 什么是防止node.js脚本终止的最有效的方法?