Rectangle 27 0

javascript Nodejs Event Loop?


eio_custom(Task,FLAG,AfterTask,Eio_REQUEST);

@Abhishek From Doc process.nextTick - On the next loop around the event loop call this callback. This is not a simple alias to setTimeout(fn, 0), it's much more efficient. Which event loop does this refer to? V8 event loop?

@Raynos libuv aims to make sure its x-platfousing multiple libraries . Right ? hence using libuv is a good idea

Basically what happens inside the node.js is that v8 loop runs and handles all javascript parts as well as C++ modules [ when they are running in a main thread ( as per official documentation node.js itself is single threaded) ]. When outside of the main thread, libev and libeio handle it in the thread pool and libev provide the interaction with the main loop. So from my understanding, node.js has 1 permanent event loop: that's the v8 event loop. To handle C++ async tasks it's using a threadpool [via libeio & libev ].

I have been personally reading the source code of node.js & v8.

Is there a way to 'see' this event que? Id like to be able to see the order of calls on the stack and see new functions being pushed there to better understand what is happening... is there some variable that tells you whats been pushed to the event que?

Of course libuv is a good idea

To see how libeio is used with node.js in order to create async modules you should see this example.

To understand just the javascript event loop you should consider watching these videos

What I am posting here is my understanding of node.js and this might be a bit off track as well.

Which appears in all modules is usually calling the function Task in the threadpool. When it's complete, it calls the AfterTask function in the main thread. Whereas Eio_REQUEST is the request handler which can be a structure / object whose motive is to provide communication between the threadpool and main thead.

Note
Rectangle 27 0

javascript Nodejs Event Loop?


Node applications run in a single-threaded event-driven model. However, Node implements a thread pool in the background so that work can be performed.

Node.js adds work to an event queue and then has a single thread running an event loop pick it up. The event loop grabs the top item in the event queue, executes it, and then grabs the next item.

Node.js uses event callbacks to avoid having to wait for blocking I/O. Therefore, any requests that perform blocking I/O are performed on a different thread in the background.

The event loop starts to encouner problems when our application functions block on I/O.

When an event that blocks I/O is retrieved from the event queue, Node.js retrieves a thread from the thread pool, and executes the function there instead of on the main event loop thread. This prevents the blocking I/O from holding up the rest of the events in the event queue.

When executing code that is longer lived or has blocking I/O, instead of calling the function directly, it adds the function to the event queue along with a callback that will be executed after the function completes. When all events on the Node.js event queue have been executed, the Node.js application terminates.

Note
Rectangle 27 0

javascript Nodejs Event Loop?


>        timers         
  
  
       I/O callbacks     
  
  
       idle, prepare     
        
           incoming:   
           poll          <  connections, 
           data, etc.  
        
          check          
  
  
    close callbacks
In the node-v0.9.0 version of libuv libev was removed
changelog
setImmediate()
socket.on('close', ...)
  • I/O callbacks: executes almost all callbacks with the exception of
  • close callbacks, the ones scheduled by timers, and setImmediate(). idle, prepare: only used internally.
  • timers: this phase executes callbacks scheduled by setTimeout() and setInterval().

@intekhab, Per this link, I think the libuv based on libeio could be used as event loop in node.js.

Also one picture which describe the Event Loop in Node.js by @BusyRich

Between each run of the event loop, Node.js checks if it is waiting for any asynchronous I/O or timers and shuts down cleanly if there are not any.

The following diagram shows a simplified overview of the event loop's order of operations.

The node.js project began in 2009 as a JavaScript environment decoupled from the browser. Using Googles V8 and Marc Lehmanns libev, node.js combined a model of I/O evented with a language that was well suited to the style of programming; due to the way it had been shaped by browsers. As node.js grew in popularity, it was important to make it work on Windows, but libev ran only on Unix. The Windows equivalent of kernel event notification mechanisms like kqueue or (e)poll is IOCP. libuv was an abstraction around libev or IOCP depending on the platform, providing users an API based on libev. In the node-v0.9.0 version of libuv libev was removed.

note: each box will be referred to as a "phase" of the event loop.

Note
Rectangle 27 0

javascript Nodejs Event Loop?


@Abhishek From Doc process.nextTick - On the next loop around the event loop call this callback. This is not a simple alias to setTimeout(fn, 0), it's much more efficient. Which event loop does this refer to? V8 event loop?

@Raynos libuv aims to make sure its x-platfousing multiple libraries . Right ? hence using libuv is a good idea

Basically what happens inside the node.js is that v8 loop runs and handles all javascript parts as well as C++ modules [ when they are running in a main thread ( as per official documentation node.js itself is single threaded) ]. When outside of the main thread, libev and libeio handle it in the thread pool and libev provide the interaction with the main loop. So from my understanding, node.js has 1 permanent event loop: that's the v8 event loop. To handle C++ async tasks it's using a threadpool [via libeio & libev ].

I have been personally reading the source code of node.js & v8.

Is there a way to 'see' this event que? Id like to be able to see the order of calls on the stack and see new functions being pushed there to better understand what is happening... is there some variable that tells you whats been pushed to the event que?

Of course libuv is a good idea

To see how libeio is used with node.js in order to create async modules you should see this example.

To understand just the javascript event loop you should consider watching these videos

What i am posting here is my understanding of node.js and this might be a bit off track as well.

Which appears in all modules is usually calling the function Task in the threadpool. When it's complete, it calls the AfterTask function in the main thread. Whereas Eio_REQUEST is the request handler which can be a structure / object whose motive is to provide communication between the threadpool and main thead.

Note
Rectangle 27 0

javascript Nodejs Event Loop?


  • Any time a CPU bound operation is encountered, it is executed inline (real machine), in its completeness.
  • Any time an I/O bound operation is encountered, the request, and its completion handler are registered with an 'event machinery' (virtual machine)
  • Finally, when there is nothing left in the system to execute, node pass the control to libuv
  • Items above are repeated many times, until all the non-I/O code are executed, and all the I/O code are registered will the libuv.
  • Load the binary, and jump into the entry point.
  • Many I/O and timer related code have native wraps. For example, network I/O.
  • Repeat the operations in the same manner above until the script ends. CPU bound operation - execute in-line, I/O bound ones, request to the machinery as above.
  • Script engine starts execution of the script.
  • So I/O calls are routed from the script to C++ bridges, with the I/O handle and the completion handler passed as arguments.
  • The compiled code executes the CPU bound activities in-line, using programming primitives.
  • The loop internally maintains an ever increasing timer. When application requests for a deferred callback(such as setTimeout), this internal timer value is leveraged to compute the right time for firing the callback.
  • The native code exercises the libuv loop. It acquires the loop, enqueues a low level event which represents the I/O, and a native callback wrapper into the libuv loop structure.
  • The native code returns to the script - no I/O is taken place at the moment!
  • Those which are are ready for I/O in a non-blocking mode, are picked up, I/O performed, and their callbacks issued. One after the other.
  • Those which are not yet ready (for example a socket read, for which the other end point hasn't written anything yet) will continued to be probed with the OS until they are available.
  • When I/O completes, the listeners are called back.
  • libuv gets into action, it picks up all the registered events, queries the operating system to get their operability.

Let me try to explain the working of event driven model with the help of an abstract example, in an abstract UNIX environment, in Node's context, as of today.

Looks like some of the entities discussed (eg: libev etc.) have had lost relevance, due to the fact that it has been a while, but I think the question still has great potential.

The event machinery above is called libuv AKA event loop framework. Node leverages this library to implement its event driven programming model.

While most of the functionalities are catered to in this manner, some (async versions) of the file operations are carried out with the help of additional threads, well integrated into the libuv. While network I/O operations can wait in expectation of an external event such as the other endpoint responding with data etc. the file operations need some work from node itself. For example, if you open a file and wait for the fd to be ready with data, it won't happen, as no one is reading actually! At the same time, if you read from the file inline in the main thread, it can potentially block other activities in the program, and can make visible problems, as file operations are very slow compared to cpu bound activities. So internal worker threads (configurable through UV_THREADPOOL_SIZE environment variable) are employed to operate on files, while the event driven abstraction works intact, from the program's perspective.

Note
Rectangle 27 0

javascript Nodejs Event Loop?


There is only one event loop provided by libuv, V8 is just a JS runtime engine.

Note