简体   繁体   中英

Understanding Asynchronous IO: vs asynchronous programming

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM