Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an event-loop and how is it different than using other models?

I have been looking into Node.JS and all the documentation and blogs talk about how it uses an event-loop rather than a per-request model.

I am having some confusion understanding the difference. I feel like I am 80% there understanding it but not fully getting it yet.

like image 920
cduruk Avatar asked Jul 17 '10 16:07

cduruk


People also ask

What is an event loop explain briefly?

In computer science, the event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program.

Why is event looping important?

Event Loop's job is to watch Callback Queue and Call Stack, whenever Call Stack is empty and we have called in Callback Queue. The event loop will move calls to Call Stack and execute them in succession.

What is an event loop in JavaScript and how does it relate to concurrency?

Event loop in JavaScript is a mechanism through which the 'calls waiting for execution' in the callback queue/job queue can be put on the call stack. For any event from the callback queue/job queue to come to call stack, the call stack will have to be empty.

What is event loop what is the difference between call stack and task queue?

Event Loop has pretty specific work. It has responsibility to see weather the call-stack is empty and does the task queue contains pending task to process. If the call-stack is empty, it will push the task to the call-stack from the queue and the task gets processed.


2 Answers

A threaded model will spawn a new thread for every request. This means that you get quite some overhead in terms of computation and memory. An event loop runs in a single thread, which means you don't get the overhead.

The result of this is that you must change your programming model. Because all these different things are happening in the same thread, you cannot block. This means you cannot wait for something to happen because that would block the whole thread. Instead you define a callback that is called once the action is complete. This is usually referred to as non-blocking I/O.

Pseudo example for blocking I/O:

row = db_query('SELECT * FROM some_table');
print(row);

Pseudo example for non-blocking I/O:

db_query('SELECT * FROM some_table', function (row) {
  print(row);
});

This example uses lambdas (anonymous functions) like they are used in JavaScript all the time. JS makes heavy use of events, and that's exactly what callbacks are about. Once the action is complete, an event is fired which triggers the callback. This is why it is often referred to as an evented model or also asynchronous model.

The implementation of this model uses a loop that processes and fires these events. That's why it is called an event queue or event loop.

Prominent examples of event queue frameworks include:

  • EventMachine (Ruby)
  • Tornado (Python)
  • node.js (V8 server-side JavaScript)
like image 151
igorw Avatar answered Oct 17 '22 03:10

igorw


Think of incoming requests or callbacks as events, that are enqueued and processed.

That is exactly the same what is done in most of the GUI systems. The system can't know when a user will click a button or do some interaction. But when he does, the event will propagated to the event loop, which is basically a loop that checks for new events in the queue and process them.

The advantage is, that you don't have to wait for results for yourself. Instead, you register callback functions that are executed when the event is triggered. This allows the framework to handle I/O stuff and you can easily rely on it's internal efficiency when dealing with long-taking actions instead of blocking processes by yourself.

In short, everythings runs in parallel but your code. There will never be two fragments of callback functions running concurrently – the event loop is a single thread. The processes that execute stuff externally and finally propagate events however can be distributed in multiple threads/processes.

like image 36
b_erb Avatar answered Oct 17 '22 04:10

b_erb