Node.js uses a well-known event loop, but does it work by polling? Some have that impression.
An event loop1 works by requesting its events from a message pump (per Wikipedia).
Here's how the event loop is implemented: 'Internally, node.js relies on libev to provide the event loop, which is supplemented by libeio[,] which uses pooled threads to provide asynchronous I/O.'2.
Here's Wikipedia's article on polling3 and another definition4.
Now, 'poll' is a system call which asks Unix to check a set of file descriptors:
'poll, ppoll - wait for some event on a file descriptor...If none of the events requested (and no error) has occurred for any of the file descriptors, then poll() blocks until one of the events occurs.'5
Possibly, the system call's name may have misled people into thinking a userland program is doing polling. Nevertheless, when the 'poll' (Unix system call) is invoked, this is not polling in itself.
Hypothetically, in order to get information from a message pump, an event loop could employ the Unix system call 'poll' to check a file descriptor, to which the message pump would write events.
Ultimately, this may be the source of the conceptual confusion here, or it may be caused by the fact that (actual) polling is the easiest method to think of, when programming.
For our case in particular, if an event loop calls Unix 'poll', this is not an instance of the event loop polling anything. Neither node.js, nor any event loop, but only Unix, polls the file descriptors (if indeed it even really does, anymore).
Anyway, an event loop, such as node.js's, does not poll its message pump. Instead, it merely makes a (blocking) request to it. Calling just any request 'polling' pollutes the meaning of the word (and that may be happening here.)
tl;dr – So, let's try anymore not to say that node.js is polling its events—okay? Instead, let's simply say that node.js waits for its events. (A lost cause, I know—but at least I've said it.)
1 http://en.wikipedia.org/wiki/Event_loop
2 http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
3 http://en.wikipedia.org/wiki/Polling_%28computer_science%29
4 http://whatis.techtarget.com/definition/polling
5 http://linux.die.net/man/2/poll
Copyright (c) 2012 Mark D. Blackwell.
Wednesday, November 14, 2012
Node.js event loop does not poll
Labels:
async,
computer,
efficient,
event,
EventMachine,
javascript,
long-poll,
loop,
message,
node.js,
programming,
unix
Wednesday, November 7, 2012
Install Opa language on 32-bit Debian squeeze, howto
The coolest feature of the Opa web programming language is that it automatically divides developers' programs into server and client sides, compiling to JavaScript.
Though the Opa compiler (as of this writing) doesn't have a 32-bit binary for Windows, I got it working in an easy way on (32-bit) Debian squeeze, after upgrading my nodejs installation.
Following Opa's instructions to install as a user (under the heading, Other Linux Distribution), I downloaded and ran their 32-bit Linux self-extracting package. When prompted, I chose to install it into ~/progra/mlstate-opa.
Then, after navigating to A tour of Opa in the sidebar, under the heading, Easy Workflow, I found and typed into a file, 'hello.opa' their sample program. The command:
$ opa hello.opa --
errored out, asking for more npm modules to be installed.
Rather than exactly following their suggested course of action, which would have installed node modules to root-owned directories, I typed:
$ npm install mongodb formidable nodemailer simplesmtp imap
After that the compiler worked just fine.
Copyright (c) 2012 Mark D. Blackwell.
Though the Opa compiler (as of this writing) doesn't have a 32-bit binary for Windows, I got it working in an easy way on (32-bit) Debian squeeze, after upgrading my nodejs installation.
Following Opa's instructions to install as a user (under the heading, Other Linux Distribution), I downloaded and ran their 32-bit Linux self-extracting package. When prompted, I chose to install it into ~/progra/mlstate-opa.
Then, after navigating to A tour of Opa in the sidebar, under the heading, Easy Workflow, I found and typed into a file, 'hello.opa' their sample program. The command:
$ opa hello.opa --
errored out, asking for more npm modules to be installed.
Rather than exactly following their suggested course of action, which would have installed node modules to root-owned directories, I typed:
$ npm install mongodb formidable nodemailer simplesmtp imap
After that the compiler worked just fine.
Copyright (c) 2012 Mark D. Blackwell.
Friday, November 2, 2012
PC timer for online tests, howto
I needed a timer to take online tests with (on an IBM PC).
And I found PC Timer. It seems ideal for this purpose:
http://www.brothersoft.com/the-pc-timer-1862.html
In order to alert me when a timed test is nearly over, I configured it to run this simple batch file:
@echo off
mode con: cols=100 lines=8
color 4e
echo .
echo .
echo "Time's up"
echo .
echo .
pause
Simple!
Presumably, it will be useful in timing other things, as well.
Beyond the timer, it also has two alarms (for moments, rather than durations, as of version 4.0).
Copyright (c) 2012 Mark D. Blackwell.
And I found PC Timer. It seems ideal for this purpose:
http://www.brothersoft.com/the-pc-timer-1862.html
In order to alert me when a timed test is nearly over, I configured it to run this simple batch file:
@echo off
mode con: cols=100 lines=8
color 4e
echo .
echo .
echo "Time's up"
echo .
echo .
pause
Simple!
Presumably, it will be useful in timing other things, as well.
Beyond the timer, it also has two alarms (for moments, rather than durations, as of version 4.0).
Copyright (c) 2012 Mark D. Blackwell.
Subscribe to:
Posts (Atom)