Questions tagged [event-loop]

Event loop refers to an infinite cycle of actions which is used for processing data based on callbacks and messages.

An event loop watches for I/O and timer state changes. It queues functions while waiting for unavailable resources, then dispatches them when resources are available.

References

1046 questions
1079
votes
19 answers

Why is setTimeout(fn, 0) sometimes useful?

I've recently run into a rather nasty bug, wherein the code was loading a had a pre-selected value. In IE6, we already had code to fix the selected
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
254
votes
6 answers

Difference between microtask and macrotask within an event loop context

I've just finished reading the Promises/A+ specification and stumbled upon the terms microtask and macrotask: see http://promisesaplus.com/#notes I've never heard of these terms before, and now I'm curious what the difference could be? I've already…
NicBright
  • 7,289
  • 5
  • 18
  • 19
153
votes
4 answers

Understanding the Event Loop

I am thinking about it and this is what I came up with: Let's see this code below: console.clear(); console.log("a"); setTimeout(function(){console.log("b");},1000); console.log("c"); setTimeout(function(){console.log("d");},0); A request comes in,…
Tarik
  • 79,711
  • 83
  • 236
  • 349
147
votes
8 answers

Nodejs Event Loop

Are there internally two event loops in nodejs architecture? libev/libuv v8 javascript event loop On an I/O request does node queue the request to libeio which in turn notifies the availability of data via events using libev and finally those…
Tamil
  • 5,260
  • 9
  • 40
  • 61
113
votes
5 answers

What exactly is a Node.js event loop tick?

I've been getting more into the internals of the Node.js architecture, and a term I see coming up a lot is "tick" as in "next tick of the event loop" or the function nextTick(). What I haven't seen is a solid definition of what exactly a "tick" is.…
d512
  • 32,267
  • 28
  • 81
  • 107
74
votes
7 answers

How would you implement a basic event-loop?

If you have worked with gui toolkits, you know that there is a event-loop/main-loop that should be executed after everything is done, and that will keep the application alive and responsive to different events. For example, for Qt, you would do…
fengshaun
  • 2,100
  • 1
  • 16
  • 25
74
votes
5 answers

Tkinter: How to use threads to preventing main event loop from "freezing"

I have a small GUI test with a "Start" button and a Progress bar. The desired behavior is: Click Start Progressbar oscillates for 5 seconds Progressbar stops The observed behavior is the "Start" button freezes for 5 seconds, then a Progressbar is…
Dirty Penguin
  • 4,212
  • 9
  • 45
  • 69
71
votes
5 answers

Async function with +=

let x = 0; async function test() { x += await 5; console.log('x :', x); } test(); x += 1; console.log('x :', x); The values of x logged are 1 and 5. My question is: why is the value of x 5 on second log? If the test is executed after x…
Aldrin
  • 2,036
  • 15
  • 12
36
votes
6 answers

What is the difference between "event loop queue" and "job queue"?

I can not understand how the following code run. Why "1" is after "b" but "h" is after "3"? Should'n the order be: a, b, 1, 2, h, 3? Some articles said that the difference between "event loop queue" and "job queue" leads to the following output. But…
Sam Yang
  • 559
  • 2
  • 6
  • 10
33
votes
2 answers

asyncio: Is it possible to cancel a future been run by an Executor?

I would like to start a blocking function in an Executor using the asyncio call loop.run_in_executor and then cancel it later, but that doesn't seem to be working for me. Here is the code: import asyncio import time from concurrent.futures import…
Brendan Maguire
  • 4,188
  • 4
  • 24
  • 28
32
votes
1 answer

Node.js event loop understanding (with a diagram)

I've read this and this, watched this... I've made a diagram of how I understand it: Javascript callbacks (functions) can be present in the current queue, check queue, close callbacks queue, timers queue and I/O callbacks queue. Js code gets…
grabantot
  • 2,111
  • 20
  • 31
29
votes
2 answers

What is the relationship between event loop and Promise

I am curious about the relationship between Event Loop and Promise. The demo exposes the question. I expected the p1 fulfilled appear in the middle, since they queue a task to the same task queue and are executed one by one. var p1 = new…
Ye Shiqing
  • 1,519
  • 2
  • 15
  • 24
29
votes
5 answers

How to integrate Boost.Asio main loop in GUI framework like Qt4 or GTK

Is there any way to integrate Boost.Asio with Qt4 (preferred) or GTK main loop? GTK provides poll(2) like API so technically is should be possible. Qt provides its own networking layer, however I prefer to use existing code written for Boost.Asio. I…
Artyom
  • 31,019
  • 21
  • 127
  • 215
28
votes
4 answers

Does this JavaScript example create “race conditions”? (To the extent that they can exist in JavaScript)

I am aware JavaScript is single-threaded and technically can’t have race conditions, but it supposedly can have some uncertainty because of async and the event loop. Here’s an oversimplified example: class TestClass { // ... async…
hryrbn
  • 303
  • 3
  • 11
26
votes
2 answers

How does Python's Twisted Reactor work?

Recently, I've been diving into the Twisted docs. From what I gathered, the basis of Twisted's functionality is the result of it's event loop called the "Reactor". The reactor listens for certain events and dispatches them to registered callback…
deadlock
  • 7,048
  • 14
  • 67
  • 115
1
2 3
69 70