Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent the NodeJS event loop from exiting?

Tags:

node.js

I've implemented a native function which takes a callback. NodeJS knows the interface, but it doesn't know anything about its implementation. This native function receives a callback and will call it when the result is ready. I don't want the event loop to exit while the callback hasn't been called.

Here is an example of such a problem.

Currently I need to do some I/O (even if it's a dumb timeout) to force NodeJS to wait for my function.

In Boost.Asio I'd just instantiate a work object and destroy it when the callback is called. Boost.Asio's event loop wouldn't exit while this object is kept around. Is there a similar approach for NodeJS? What do I use in NodeJS (bonus if your answer doesn't mention timers)?

like image 502
vinipsmaker Avatar asked Aug 22 '16 14:08

vinipsmaker


People also ask

How do I stop NodeJS from exiting?

There is no way to prevent the exiting of the event loop at this point, and once all 'exit' listeners have finished running the Node.js process will terminate.

How do you stop an infinite loop in NodeJS?

For NodeJS only – Use process. abort() or process. exit().

What is blocking the event loop?

What is Blocking EventLoop? If a thread is taking a long time to execute a callback (Event Loop) or a task (Worker), it's called “blocked”. While a thread is blocked working on behalf of one client, it cannot handle requests from any other clients.

How do I keep node running app?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.


1 Answers

The best way to do that would be writing a C++ addon and using one of the handles offered by libuv (of course, the one that fits your requirements - see the official documentation for further details).

If you don't want to do that or if you can't do that (that is the case, if I've correctly understood the question), a viable solution not mentioned in the other answers is using process.nextTick to schedule a function that checks at each tick if the loop can expires or not.
see here for further details about process.nextTick.

As a minimal, working, never-ending example:

var process = require('process')
var stop = false;
var f = function() { if(!stop) process.nextTick(f) }
f()

This way your function is in charge of setting the stop control variable once it finishes to execute, then the loop will stop.

If you have multiple callbacks to wait for, simply use a counter and check it looking for 0.
If you don't want to explicitly set and update the value of the counter each time you add a new function (error-prone), you can easily write a launcher to start your functions that increments the counter and schedules a check on the next tick if needed.
You could also pass a callback as an extra argument to your functions to notify when they are over, so that they have not to deal explicitly with the counter itself.

A plus of using a dedicated function that is scheduled on the next tick is that it's clear to the reader what you are doing.
On the other side, a fake server, a timeout scheduled in the future or an I/O stream resumed and never used are quite obscure, for the reader doesn't know immediately why you are doing that.

like image 99
skypjack Avatar answered Oct 13 '22 21:10

skypjack