0

I'm assuming that the definition of asynchronous is as follows.

  • Let's start with a relationship between two 'things': X and Y.
    • They can be anything, e.g. X can be you and Y can be your washing machine.
    • Let's say X requests something of Y.
      • This can also be anything: a question, a task.
      • Let's say we live in a world where Y cannot immediately respond with the answer / completion status.
        • What happens?
          • In a Synchronous relationship, you 'wait around' in some way.
            • This could involve just sitting there or asking repeatedly.
          • In an Asynchronous relationship, you go on with your life.
            • Y will ping you when it's done.

From the perspective of a user's API, node.js and asyncio seem asynchronous. For example, in node.js you can register callbacks upon completion of certain events. And in asyncio, the callback logic goes right after some await my_io().

But here's my question - are node.js and asyncio actually truly asynchronous? Implementation-wise, do they just engage in a bunch of frantic non-blocking "hey, is this file descriptor free yet?" calls or is it actually interrupt-driven?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Noah Stebbins
  • 385
  • 1
  • 11

1 Answers1

1

Yes, they're truly asynchronous by your definition, you (i.e. the Node engine/Python interpreter) go on doing other work while waiting for the task to complete.

How it's implemented doesn't matter to you--you trust that the designers made the right design decisions on your behalf. If there is frantic "hey is this file descriptor free yet?" going on (that's called "polling" or "spin waiting"), that's an implementation detail handled by the engine which has dispatched a thread to wait.

Incidentally, busy waiting is sometimes an efficient way to wait for a resource in certain circumstances (generally, when you expect it to be available very soon) as an alternative to an interrupt or notification.

Think of it this way: if you're waiting for your washing machine to finish a load and you want to be notified precisely (or as precisely as possible) when it finishes as you go about other tasks, you could call your friend to watch the laundry for you. The contract is that your friend notifies you as soon as possible when the laundry is done, but you don't care how they do it. Maybe they stand next to the machine and check if it's done constantly (a good idea if the load is almost complete), or maybe they're clever and have devised a system so they can do other work without constantly checking. Either way, it doesn't impact your ability to do other tasks.

The "friend" is like a thread dispatched by the Node engine and the "laundry" might be a file, HTTP response, etc. From your perspective, it's truly asynchronous--the work spent to fulfill the resource request is being done by a thread spawned by the runtime, the OS or network and runs in parallel to your process.

See this answer for diagrams showing the distinction between asynchrony and synchrony.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • I'd consider "calling a friend" as just passing the burden on, but not getting to the root of whether the implementation is async / sync. If we pass the burden on to the friend, then I'm interested in knowing whether the friend in node.js and asyncio employs a synchronous solution (e.g. busy wait) or an async solution (e.g. interrupt). – Noah Stebbins Feb 14 '21 at 18:08
  • 1
    Yeah, if you want work done, someone has to do it--it doesn't magically get done otherwise. The question of async is purely whether _you_ are waiting ("blocking") or not for other entities to do a job. If the OS is reading files from disk (very slow!), why sit there waiting for that when you can get stuff done in parallel? As for your second question, it's implementation-dependent. Probably it's mostly interrupts, but it's possible that node also uses spin locks or polling for certain things. You'd have to look at the source relevant for the thing you care about and see how it's implemented. – ggorlen Feb 14 '21 at 18:11