06.07.2017 Views

Mastering JavaScript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9<br />

If a program blocks I/O, the Node server will refuse new requests. There are several<br />

ways of solving this problem. The most popular traditional approach is to use<br />

several threads to process requests—this technique is known as multithreading.<br />

If are you familiar with languages such as Java, chances are that you have written<br />

multithreaded code. Several languages support threads in various forms—a thread<br />

essentially holds its own memory and state. Writing multithreaded applications on a<br />

large scale is tough. When multiple threads are accessing a common shared memory<br />

or values, maintaining the correct state across these threads is a very difficult task.<br />

Threads are also costly when it comes to memory and CPU utilization. Threads that<br />

are used on synchronized resources may eventually get blocked.<br />

The browser handles this differently. I/O in the browser happens outside the main<br />

execution thread and an event is emitted when I/O finishes. This event is handled by<br />

the callback function associated with that event. This type of I/O is non-blocking and<br />

asynchronous. As I/O is not blocking the main execution thread, the browser can<br />

continue to process other events as they come without waiting on any I/O. This is a<br />

powerful idea. Asynchronous I/O allows browsers to respond to several events and<br />

allows a high level of interactivity.<br />

Node uses a similar idea for asynchronous processing. Node's event loop runs as<br />

a single thread. This means that the application that you write is essentially singlethreaded.<br />

This does not mean that Node itself is single-threaded. Node uses libuv<br />

and is multithreaded—fortunately, these details are hidden within Node and you<br />

don't need to know them while developing your application.<br />

Every call that involves an I/O call requires you to register a callback. Registering a<br />

callback is also asynchronous and returns immediately. As soon as an I/O operation<br />

is completed, its callback is pushed on the event loop. It is executed as soon as<br />

all the other callbacks that were pushed on the event loop before are executed.<br />

All operations are essentially thread-safe, primarily because there is no parallel<br />

execution path in the event loop that will require synchronization.<br />

Essentially, there is only one thread running your code and there is no parallel<br />

execution; however, everything else except for your code runs in parallel.<br />

Node.js relies on libev (http://software.schmorp.de/pkg/libev.html) to<br />

provide the event loop, which is supplemented by libeio (http://software.<br />

schmorp.de/pkg/libeio.html) that uses pooled threads to provide asynchronous<br />

I/O. To learn even more, take a look at the libev documentation at http://pod.tst.<br />

eu/http://cvs.schmorp.de/libev/ev.pod.<br />

[ 203 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!