Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous Python for the Complete Beginner

Asynchronous Python for the Complete Beginner

Talk given at PyCon 2017 on 5/21/2017

With the introduction of the asyncio package in Python 3.4, you can hear lots of people talking about asynchronous programming, most in a favorable way, some not so much. In this talk, I will tell you what this async fever is about and what can it do for you that regular Python can't, not only with asyncio, but also with other frameworks that existed long before it.

Miguel Grinberg

May 21, 2017
Tweet

More Decks by Miguel Grinberg

Other Decks in Programming

Transcript

  1. Multiple Processes • The OS does all the multi-tasking work

    • Only option for multi-core concurrency
  2. Multiple Threads • The OS does all the multi-tasking work

    • In CPython, the GIL prevents multi-core concurrency
  3. Synchronous Chess Exhibition • Assumptions: - 24 opponents - Polgár

    moves in 5 seconds - Opponents move in 55 seconds - Games average 30 move pairs • Each game runs for 30 minutes • 24 sequential games would take 24 x 30 min = 720 min = 12 hours!!! Photo by Ed Yourdon Simultaneous chess exhibition by Judit Polgár, 1992
  4. Asynchronous Chess Exhibition • Polgár moves on first game •

    While opponent thinks, she moves on second game, then third, fourth... • A move on all 24 games takes her 24 x 5 sec = 120 sec = 2 min • After she completes the round, the first game is ready for her next move! • 24 games are completed in 2 min x 30 = 60 min = 1 hour! Photo by Ed Yourdon Simultaneous chess exhibition by Judit Polgár, 1992
  5. A practical definition of Async... “A style of concurrent programming

    in which tasks release the CPU during waiting periods, so that other tasks can use it.”
  6. Suspend and Resume • Async functions need the ability to

    suspend and resume • A function that enters a waiting period is suspended, and only resumed when the wait is over • Four ways to implement suspend/resume in Python: - Callback functions - Generator functions - Async/await (Python 3.5+) - Greenlets (requires greenlet package)
  7. Scheduling Asynchronous Tasks • Async frameworks need a scheduler, usually

    called “event loop” • The loop keeps track of all the running tasks • When a function is suspended, return controls to the loop, which then finds another function to start or resume • This is called “cooperative multi-tasking”
  8. from time import sleep def hello(): print('Hello') sleep(3) print('World!') if

    __name__ == '__main__': hello() Example: Standard (synchronous) Python Print “hello”, wait three seconds, then print “world”
  9. Example: Asyncio Print “hello”, wait three seconds, then print “world”

    import asyncio loop = asyncio.get_event_loop() @asyncio.coroutine def hello(): print('Hello') yield from asyncio.sleep(3) print('World!') if __name__ == '__main__': loop.run_until_complete(hello()) import asyncio loop = asyncio.get_event_loop() async def hello(): print('Hello') await asyncio.sleep(3) print('World!') if __name__ == '__main__': loop.run_until_complete(hello())
  10. Example: Greenlet Frameworks Print “hello”, wait three seconds, then print

    “world” from gevent import sleep def hello(): print('Hello') sleep(3) print('World!') if __name__ == '__main__': hello() from eventlet import sleep def hello(): print('Hello') sleep(3) print('World!') if __name__ == '__main__': hello()
  11. CPU Heavy Tasks • Long CPU-intensive tasks must routinely release

    the CPU to avoid starving other tasks • This can be done by “sleeping” periodically, such as once per loop iteration • To tell the loop to return control back as soon as possible, sleep for 0 seconds • Example: await asyncio.sleep(0)
  12. Async and the Python Standard Library • Blocking library functions

    are incompatible with async frameworks socket.*, select.* subprocess.*, os.waitpid threading.*, multiprocessing.* time.sleep • All async frameworks provide non-blocking replacements for these • Eventlet and Gevent can “monkey-patch” the standard library to make it async compatible
  13. Processes vs. Threads vs. Async Processes Threads Async Optimize waiting

    periods Yes (preemptive) Yes (preemptive) Yes (cooperative) Use all CPU cores Yes No No Scalability Low (ones/tens) Medium (hundreds) High (thousands+) Use blocking std library functions Yes Yes No GIL interference No Some No