0

I'm having a difficult time understanding asynchronous IO so I hope to clear up some of my misunderstanding because the word "asynchronous" seems to be thrown in a lot. If it matters, my goal is to get into twisted python but I want a general understanding of the underlying concepts.

What exactly is asynchronous programming? Is it programming with a language and OS that support Asynchronous IO? Or is it something more general? In other words, is asynchronous IO a separate concept from asynchronous programming?

puketronic
  • 95
  • 2
  • 7
  • Your questions are very broad. SO is a site that strongly prefers specific question. You should narrow it down to one thing. – Klaus D. May 31 '16 at 03:08
  • I disagree, the question is fine. – Evan Carroll May 31 '16 at 03:34
  • There is a similar post more specific to node.js [What is the difference between synchronous and asynchronous programming (in node.js)](http://stackoverflow.com/questions/16336367/what-is-the-difference-between-synchronous-and-asynchronous-programming-in-node) – Altronicx May 31 '16 at 03:42

1 Answers1

3

Asynchronous IO means the application isn't blocked when your computer is waiting for something. The definition of waiting here is not processing. Waiting for a webserver? Waiting for a network connection? Waiting for a hard drive to respond with data on a platter? All of this is IO.

Normally, you write this in a very simple fashion synchronously:

let file = fs.readFileSync('file');
console.log(`got file ${file}`);

This will block, and nothing will happen until readFileSync returns with what you asked for. Alternatively, you can do this asynchronously which won't block. This compiles totally differently. Under the hood it may be using interrupts. It may be polling handles with select statements. It typically uses a different binding to a low level library, such as libc. That's all you need to know. That'll get your feet wet. Here is what it looks like to us,

fs.readFile(
  'file',
  function (file) {console.log(`got file ${file}`)}
);

In this you're providing a "callback". That function will request the file immediately, and when it (the function you called, here fs.readFile) gets the file back it will call your callback (here that's a function that takes a single argument file.

There are difficulties writing things asynchronously:

  • Creates pyramid code if using callbacks.
  • Errors can be harder to pinpoint.
  • Garbage collection isn't always as clean.
  • Performance overhead, and memory overhead.
  • Can create hard to debug situations if mixed with synchronous code.

All of that is the art of asynchronous programming..

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • Your example might be a little confusing, since neither Python's asyncio nor Twisted, nor many other async/event-driven libraries (eg. Qt, GTK) do asynchronous file system reads out of the box. A read from a network socket might be a better example, because all serious async stacks do that. – detly May 31 '16 at 04:11
  • Yea, I can see that. It's a valid point. Hopefully my answer is of some use. The question itself wasn't specific to python so I did my best. But, come on Python, 2016 and stuff. – Evan Carroll May 31 '16 at 04:22
  • 1
    Don't get me started. Anyway, I only bring it up because a beginner reading this might make their first association with async programming "reading files", which can influence what they then look for or attempt as an introductory project. – detly May 31 '16 at 04:26