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

Beyond Python's Comfort Zone: Asynchronous Pyth...

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for Alex Jerez Alex Jerez
November 18, 2016

Beyond Python's Comfort Zone: Asynchronous Python in 2016-2017

Python was not originally created with concurrency in mind, so with rise in async in modern popular programming languages, how has Python integrated with async in 2016 and 2017 ?

Avatar for Alex Jerez

Alex Jerez

November 18, 2016
Tweet

Other Decks in Programming

Transcript

  1. About Me • Alex Jerez • Python Programmer at ABI

    Research in Oyster Bay • HTML/CSS, Javascript, Java, C#, Rust • Founded Crosshatch, a social platform, in 2012, flopped
  2. Python 2.x Python 3.x • Legacy • Still shipped as

    default for many Platforms, such as Linux (Ubuntu, Debian, Fedora) • The most common version of Python being used, but the gap is shrinking. • Support ending on 2020 (2.7) • Future-Proof • In Active Development • Library support not as good as 2.x • Async ready
  3. What is Concurrency? • Multitasking - Concurrent programs handle tasks

    that are all in progress at the same time, but it is only necessary to work briefly and separately on each task - Doing A while waiting for B • Improved Responsive Experience from end-user • Concurrency is not Parallelism ◦ Tasks are sharing the execution thread while still appearing to be executing in parallel.
  4. What is Concurrency? • Concurrency is not for all operations

    • CPU-Bound Applications - Your CPU is blocking you from doing more work • IO-Bound Applications - You’re waiting on something else (HTTP Request)
  5. Popular libraries for Concurrency • Tornado • Twisted • Gevent

    • greenlets Other Methods • Threading • Multiprocessing • Subprocess • Message Queue
  6. 1999: Python 1.5.2 ships with asyncore, asynchat 2001: Python 2.2

    ships with Generators 2006: Python 2.5 ships with sendable ‘yield’, coroutines 2012: Python 3.3 ships with ‘yield from’ 2015: Python 3.5 ships with ‘async/await’ & asyncio
  7. Generators def gen(num): if num > 1: for item in

    range(1,num): yield item pass
  8. Coroutines <coroutine>.send(value) Resumes the execution and “sends” a value into

    the generator function. The value argument becomes the result of the current yield expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value.
  9. asyncio Module introduced in Python 3.4 asyncio is the of

    a number of useful third-party python tools. • Written to tackle concurrency natively. • Added to std library • Implements a flexible interface so that it can be tied in with frameworks like Twisted and Tornado
  10. asyncio • Concise, easy to use, hard to master. •

    New builtin types for coroutines • async for and async with • Generic, can be tied to Tornado, gevent, etc. • 2x slower than a function call, 10-100x faster than a yield • @types.coroutine turns generators into coroutines • Event loops, futures, tasks, callbacks • Libraries for sockets
  11. Main asyncio functions • Asyncio.get_event_loop --> Event Loop • loop.create_task()

    --> Task • loop.run_until_complete(), loop.run_forever() • asyncio.gather() • loop.run_in_executor() • loop.close()
  12. The Event Loop • asyncio.get_event_loop ◦ run_forever() - run loop

    until stop() is called. ◦ stop() - ◦ run_until_complete(Future) - run until Future is finished.
  13. Scheduling Tasks • You cannot await a sleep function •

    asyncio.get_event_loop ◦ call_soon(callback, *args) - trigger callback asap ◦ call_later(delay, callback, *args) - delay callback trigger by delay ◦ call_at(datetime, callback, *args)
  14. asyncio in debug • It’s generally a good idea to

    run asyncio in debug mode, especially when learning asyncio ◦ loop.set_debug(True) ◦ PYTHONASYNCIODEBUG=1
  15. asyncio criticism • Armin Ronacher’s Blog Post ◦ “I don't

    understand Python's Asyncio” ◦ http://lucumr.pocoo.org/2016/10/30/i-dont-understand-asyncio/-