which callbacks are sent to the event loop?

68 views
Skip to first unread message

Wil Chung

unread,
Aug 7, 2011, 4:23:23 PM8/7/11
to nodejs
I wasn't able to find the answer elsewhere, and I couldn't find where
to start looking in the code.

How granular are the callbacks in the event loop?

Is it every http request that's in the event loop? Or is it every
callback that gets called? Or is it something inbetween, where
whomever is writing a non-blocking call needs to make some call to
node explicitly to say that this callback can be put on the event
loop?

I assume it's the 2nd one, where every callback that gets stated in my
code is put on the queue to be processed by the event loop?

Wil

Mark Hahn

unread,
Aug 8, 2011, 2:16:13 PM8/8/11
to nod...@googlegroups.com
A callback is just passing code to another function.  Node doesn't know about these and does nothing.

The event loop doesn't get control until some low-level async code is executed, like fs.write or setTimeout.  Also there is no new tick until all the executions on the current tick have been given a chance to run.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: 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 post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Isaac Schlueter

unread,
Aug 8, 2011, 3:01:45 PM8/8/11
to nod...@googlegroups.com
There are basically three things you're talking about. I'll go
through each one.

1. Event listeners
2. Callbacks passed from one JavaScript function to another.
3. Stuff waiting on something to happen from a lower level (libeio or
libev, or in the future, libuv.)

The EventEmitter stuff is all in JavaScript. For instance:

f = new EventEmitter
f.on("foo", function () { console.error("bar") })
f.emit("foo")

This never touches the event loop. It just adds the function object
to a list marked "foo", and then executes all the functions in that
list when you emit("foo"). (It's a little more involved than that for
API friendliness and performance, but not much.)


Callbacks passed from one bit of JavaScript to another is also pure-js
stuff. The difference between an "event handler" and a "callback" is
that typically callbacks are only ever called once, whereas an event
might be emitted many times. While callbacks typically are used
because there's some async operation which dips below the JS layer,
there's nothing stopping you from writing something like this:

function callRightNow (x, y, cb) {
cb(null, x + y)
}


When something has to do some IO (or sets a timer with setInterval,
setTimeout, or process.nextTick), it gets attached to the event loop.
It's not quite as straightforward as a 1:1 mapping from setTimeout
call to event loop timer. (That would be inefficient.)

When you do this:

net.createServer(reqHandler).listen(8000)

there's actually just one "thing" on the event loop: the function that
gets called whenever a new incoming TCP connection is initiated.


If you're interested in diving into the internals, it's pretty
fascinating. I'd start with src/node.cc and follow the trail.
src/node_file.cc is pretty straightforward if you're familiar with the
standard posix filesystem C functions. All the thread pool and event
loop stuff is in deps/eio and deps/ev (or deps/uv in master).

Ryan Bales

unread,
Aug 9, 2011, 9:44:59 AM8/9/11
to nod...@googlegroups.com
Thanks a lot for this comparison.  It demystified a good deal about the event loop for me.

~Ryan Bales


Ryan Bales

Compared to the clouds as they roll by
Compared to the bugs and the spiders and flies
I am an ape man
~The Kinks

Wil Chung

unread,
Aug 10, 2011, 1:02:15 PM8/10/11
to nodejs
Thanks for the detailed reply. I think that clears it up a bit.
Basically, node picks up on whenever you make lower level IO system
calls.

That also means that if I know for sure the things I'm writing aren't
going to IO of any sort, I can just write it without callbacks,
because it makes no difference to node.

And the reason it doesn't matter to node is because IO, whether to
disk or network is orders of magnitudes slower. I'll take a look at
some of the files you suggested. Thanks!

Wil

Étoile Grosvenor

unread,
Aug 10, 2011, 1:41:16 PM8/10/11
to nod...@googlegroups.com
I solved the problem i installed a other version and it works.
Thnaks
Reply all
Reply to author
Forward
0 new messages