questions about event loops

Hello! Here are some questions & answers. The goal isn't to get all the questions "right". Instead, the goal is to learn something! If you find a topic you're interested in learning more about, I'd encourage you to look it up and learn more.

if you just have 1 event loop in your program and no other code, is it possible for 2 lines of your code to run at the exact same time?

no!

all the code in an event loop runs in a single operating system thread, so only one piece of code can be running at any given time.

is it possible for there to be other threads in a program with an event loop?

yes!

for example, in node.js all the Javascript code runs in a single thread, but there are other worker threads that handle making network requests and other I/O.

in an event loop, is the operating system responsible for scheduling what order functions run in?

nope!

for example, with Python's asyncio, the code that does the scheduling is a Python program

is an event loop actually a loop? (like a for loop or while loop?)

yes!

often event loops are implemented as a while loop that looks something like this:

while True:
    self._run_once()
    if self._stopping:
        break

(the above is the literal exact code from python's asyncio event loop)

how does the event loop decide what function to run next?

it has a queue!

functions get pushed onto the queue when they're ready to run, and then the event loop runs functions from the queue in order

if a network requests returns and it has an attached callback, does that callback get pushed onto the event loop's queue?

yes!

functions can get pushed onto the event loop's queue because a network request or some other I/O finished, or the user clicked something, or because the function was scheduled to run at that time, etc

are regular functions the same as async functions?

no!

async functions are special because they can be "paused" and restarted later by the event loop. For example, in this Javascript code:

async function panda() {
    let x = 3;
    await elephant();
    let y = 4;
}
the event loop schedules elephant(), pauses panda, and schedules panda() to restart after elephant() is finished running. Normal non-async functions can't be paused and restarted like this. Another name for these async functions that can be paused and restarted is coroutines.

if you ask the event loop to run a function at a certain time (like with setTimeout in Javascript), is it guaranteed to run at that exact time?

nope!

the event loop will do its best, but sometimes functions get delayed

do promises and setTimeout and async/await and callbacks in Javascript all use the same event loop?

yes!

the syntax is different, but they're all different ways for you to schedule code to run later

in this code:
x = 3;
y = 4;
is it possible for the event loop to interrupt after x=3 and run something else?

no!

you need to yield to the event loop for it to run a different function, for example with await

if you run some CPU-intensive code, like
while(true) { 
   i = i * 3
}
will the event loop eventually interrupt the code?

no!

you can usually block the event loop for as long as you want by running something CPU-intensive.

if you're using 100% of one CPU core in a web server's event loop, will you be able to respond to HTTP requests immediately when they come in?

no!

if your event loop's CPU is always busy, then new events that come in won't get handled on time because the CPU will be busy.

does Javascript code always have an event loop?

yes!

at least in node.js and in the browser, there's always an event loop that Javascript code runs on.

it's possible that there's some other Javascript runtime that doesn't use an event loop but I don't know about it

is there a standard event loop library that all event loops use?

no!

there are a lot of different event loop implementations in different programming languages.

can you use an event loop in any programming language?

yes!

most programming languages don't have the same "everything runs on the event loop" model as Javascript, but many languages have event loop libraries. And in theory you can always write your own event loop library if it doesn't exist already.

more reading

... and you're done! To get the most out of this set of questions, consider taking a minute to write down (in your own words) one or two things you learned!

If you want, you can write down what you learned in this box and anonymously send it to me. Hearing what people are learning helps me build better questions in the future :)