Trigger the event loop to be processed

90 views
Skip to first unread message

Fredrik O

unread,
Jan 21, 2015, 6:06:30 PM1/21/15
to nod...@googlegroups.com
I need to call a function synchronously which trigger the event loop to be processed before the function returns. Pseudo code:


require('fs').exists('file', function() {
   // This function should be fired even for the while-loop below
});

while(true) {
  runEventLoop();  // process the eventloop, so all asynchronous tasks will be run anyway
}


Is there any inbuilt function for that?


Thanks in advance!

Matt

unread,
Jan 21, 2015, 8:59:51 PM1/21/15
to nod...@googlegroups.com
Yes it's called existsSync(). You don't need some fancy function to call the event loop if you just want to find a file before the rest of your code starts.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/08f6b047-e4d1-40ef-97f6-b5b91d82e198%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fredrik O

unread,
Jan 22, 2015, 2:27:44 PM1/22/15
to nod...@googlegroups.com
Sorry Matt, I was not precise enough. It was only an example. The reality is that I am building a new sort of platform above node and was curious if there is something I can invoke to force the event loop immediately to be processed.

I found UV_RUN_NOWAIT but it seems not to fire any http listeners (when a new connection is received). C++ code:

// Within a C++/v8 addon
int r = uv_run(uv_default_loop(), UV_RUN_NOWAIT);

I modified the library uvrun to use the option UV_RUN_NOWAIT, but it does not work properly. It does not fire any callbacks even if I call it within a while-loop. Any clue why? Or who I may ask?

Ben Noordhuis

unread,
Jan 22, 2015, 4:18:41 PM1/22/15
to nod...@googlegroups.com
On Thu, Jan 22, 2015 at 8:27 PM, Fredrik O <evo...@gmail.com> wrote:
> Sorry Matt, I was not precise enough. It was only an example. The reality is
> that I am building a new sort of platform above node and was curious if
> there is something I can invoke to force the event loop immediately to be
> processed.
>
> I found UV_RUN_NOWAIT but it seems not to fire any http listeners (when a
> new connection is received). C++ code:
>
> // Within a C++/v8 addon
> int r = uv_run(uv_default_loop(), UV_RUN_NOWAIT);
>
> I modified the library uvrun to use the option UV_RUN_NOWAIT, but it does
> not work properly. It does not fire any callbacks even if I call it within a
> while-loop. Any clue why? Or who I may ask?

The event loop is not re-entrant. Calling uv_run() from a callback
that itself originates from a call to uv_run() is undefined behavior,
there are no guarantees about what will happen.

Barring bugs, it should work when called from the top-level script.
However, many things inside core use process.nextTick() and that
function is pretty much an event loop in its own right, one that is
separate from the libuv event loop.

The precise semantics are convoluted but process.nextTick 'ticks'
happens around calls from C++ into JS land. If there are no such
calls, no tick callbacks run. Calls frequently originate from libuv
callbacks but:

1. If the libuv event loop is not making forward progress because it's
waiting for tick callbacks, and

2. If those tick callbacks are not happening because of a lack of C++
-> JS calls, then

3. Everything stalls!

To make a long story short, when you start meddling with the event
loop, it's just too easy to create a catch-22 situation. Hope that
helps!

Fredrik O

unread,
Jan 22, 2015, 6:43:48 PM1/22/15
to nod...@googlegroups.com
Thanks Ben for the answer. It helped me a lot :-)
Reply all
Reply to author
Forward
0 new messages