My understanding on asynchronous programming in python

August 9, 2018

Why do we need asynchronous?

Chess master Judit Polgár hosts a chess exhibition in which she plays multiple amateur players. She has two ways of conducting the exhibition: synchronously and asynchronously. Assumptions:

  • 24 opponents
  • Judit makes each chess move in 5 seconds
  • Opponents each take 55 seconds to make a move
  • Games average 30 pair-moves (60 moves total)
    Synchronous version: Judit plays one game at a time, never two at the same time, until the game is complete. Each game takes (55 + 5) * 30 == 1800 seconds, or 30 minutes. The entire exhibition takes 24 * 30 == 720 minutes, or 12 hours. Asynchronous version: Judit moves from table to table, making one move at each table. She leaves the table and lets the opponent make their next move during the wait time. One move on all 24 games takes Judit 24 * 5 == 120 seconds, or 2 minutes. The entire exhibition is now cut down to 120 * 30 == 3600 seconds, or just 1 hour. Source

Clearify some concepts

  • multiprocessing is a model of parallelism which is like throwing 2 balls at a time with 2 hands, it is CPU bound.
  • multithreading is a model of concurrency
  • asyncio is like throwing 1 ball after another without waiting the previous one coming back. It is IO bound.

How to use(Different choices)?

Event loop is like the main thread that loop forever to watch out something that it interesting happen. When that happen, execute the code that actually deal with the detail. Python utilize the asyncio package to gain an event loop.

why do we need yield from

from types import coroutine
from asyncio import coroutine
from tornado.gen import coroutine

Reference