Skip to content

Instantly share code, notes, and snippets.

@sadhasivam
Last active June 29, 2020 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sadhasivam/6d5755eff90180668aaab3bc54df4168 to your computer and use it in GitHub Desktop.
Save sadhasivam/6d5755eff90180668aaab3bc54df4168 to your computer and use it in GitHub Desktop.
event_loops

Q. 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? A. 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.


Q. is it possible for there to be other threads in a program with an event loop? A. 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.


Q. in an event loop, is the operating system responsible for scheduling what order functions run in? A. nope! for example, with Python's asyncio, the code that does the scheduling is a Python program


Q. is an event loop actually a loop? (like a for loop or while loop?) A. 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)


Q. how does the event loop decide what function to run next? A. 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


Q. if a network requests returns and it has an attached callback, does that callback get pushed onto the event loop's queue? A. 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


Q. are regular functions the same as async functions? A. 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.


Q. 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! A. the event loop will do its best, but sometimes functions get delayed


Q. do promises and setTimeout and async/await and callbacks in Javascript all use the same event loop? A. yes! the syntax is different, but they're all different ways for you to schedule code to run later


Q. in this code:

x = 3;
y = 4;

is it possible for the event loop to interrupt after x=3 and run something else?

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


Q. if you run some CPU-intensive code, like

while(true) { 
   i = i * 3
}

will the event loop eventually interrupt the code?

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


Q. 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?

A. 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.


Q. does Javascript code always have an event loop? A. 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


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

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


Q. can you use an event loop in any programming language? A. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment