title: How does asyncio actually work?
body: <p>This question is motivated by my another question: <a href="https://stackoverflow.com/questions/48989065/how-to-await-in-cdef">How to await in cdef?</a></p>
  
  <p>There are tons of articles and blog posts on the web about <code>asyncio</code>, but they are all very superficial.  I couldn't find any information about how <code>asyncio</code> is actually implemented, and what makes I/O asynchronous.  I was trying to read the source code, but it's thousands of lines of not the highest grade C code, a lot of which deals with auxiliary objects, but most crucially, it is hard to connect between Python syntax and what C code it would translate into.</p>
  
  <p>Asycnio's own documentation is even less helpful.  There's no information there about how it works, only some guidelines about how to use it, which are also sometimes misleading / very poorly written.</p>
  
  <p>I'm familiar with Go's implementation of coroutines, and was kind of hoping that Python did the same thing.  If that was the case, the code I came up in the post linked above would have worked.  Since it didn't, I'm now trying to figure out why.  My best guess so far is as follows, please correct me where I'm wrong:</p>
  
  <ol>
  <li>Procedure definitions of the form <code>async def foo(): ...</code> are actually interpreted as methods of a class inheriting <code>coroutine</code>.</li>
  <li>Perhaps, <code>async def</code> is actually split into multiple methods by <code>await</code> statements, where the object, on which these methods are called is able to keep track of the progress it made through the execution so far.</li>
  <li>If the above is true, then, essentially, execution of a coroutine boils down to calling methods of coroutine object by some global manager (loop?).</li>
  <li>The global manager is somehow (how?) aware of when I/O operations are performed by Python (only?) code and is able to choose one of the pending coroutine methods to execute after the current executing method relinquished control (hit on the <code>await</code> statement).</li>
  </ol>
  
  <p>In other words, here's my attempt at "desugaring" of some <code>asyncio</code> syntax into something more understandable:</p>
  
  <pre><code>async def coro(name):
      print('before', name)
      await asyncio.sleep()
      print('after', name)
  
  asyncio.gather(coro('first'), coro('second'))
  
  # translated from async def coro(name)
  class Coro(coroutine):
      def before(self, name):
          print('before', name)
  
      def after(self, name):
          print('after', name)
  
      def __init__(self, name):
          self.name = name
          self.parts = self.before, self.after
          self.pos = 0
  
      def __call__():
          self.parts[self.pos](self.name)
          self.pos += 1
  
      def done(self):
          return self.pos == len(self.parts)
  
  
  # translated from asyncio.gather()
  class AsyncIOManager:
  
      def gather(*coros):
          while not every(c.done() for c in coros):
              coro = random.choice(coros)
              coro()
  </code></pre>
  
  <p>Should my guess prove correct: then I have a problem.  How does I/O actually happen in this scenario? In a separate thread?  Is the whole interpreter suspended and I/O happens outside the interpreter?  What exactly is meant by I/O?  If my python procedure called C <code>open()</code> procedure, and it in turn sent interrupt to kernel, relinquishing control to it, how does Python interpreter know about this and is able to continue running some other code, while kernel code does the actual I/O and until it wakes up the Python procedure which sent the interrupt originally?  How can Python interpreter in principle, be aware of this happening?</p>