"What Is Async, How Does It Work, And When Should I Use It?" - PyCon 2014
Here is my note about an awesome talk in PyCon 2014.
Slide: https://speakerdeck.com/pycon2014/what-is-async-how-does-it-work-and-when-should-i-use-it-by-a-jesse-jiryu-davis
Video: https://www.youtube.com/watch?v=9WV7juNmyE8#t=573
Video Description: Python’s asynchronous frameworks, like Tulip, Tornado, and Twisted, are increasingly important for writing high-performance web applications. Even if you’re an experienced web programmer, you may lack a rigorous understanding of how these frameworks work and when to use them. Let’s see how Tulip’s event loop works, and learn how to efficiently handle very large numbers of concurrent connections.
What is async? (example)
CPU - bound web app
throughput bound by computation
No async
ex: subs, 路邊攤早餐,潤餅小店
Normal web app
regular old web server (big slow backend, like database or OAuth)
throughput bound by memory
async
need to store state (例如店員需要記得每個人點了什麼,會花多久時間)
ex: pizza, 7-11店員
Websocket application
Long live and event driven.
number of clients bound by memory
async
ex: Omakase(一種無菜單料理)
What is async for?
minimize resources per connection.
C10K
“It’s time for web servers to handle ten thousand clients simultaneously, don’t you think? After all, the web is a big place now.” (http://www.kegel.com/c10k.html)
C10k: a numeronym for concurrently handling ten thousand connections. (wiki: http://en.wikipedia.org/wiki/C10k_problem)
django-c10k-demo: https://github.com/aaugustin/django-c10k-demo
Why is async hard to code?
Need to “store state”
Ways to store state
Threads
done for you (with program counter, stack…)
used to that kind of programing model
easy to code
Callbacks
waiting for event
not used to using
not design to be easy for this kind of usage
Coroutines
Greenlets
…
but no one of these tech have satisfied everyone yet :P
What is async? (summary)
single-threaded
I/O concurrency
Non-blocking sockets
blocking:
By default, TCP sockets are in “blocking” mode.
A blocking call does not return to your program until the event you requested has been completed.
nonblocking:
A nonblocking socket allows input/output operation on a channel without blocking the processes using it.
Nonblocking calls return to your program immediately to reveal whether the requested service was completed. An error number may mean that your call would have blocked had it been a blocking call, but asynchronous is put into a WAIT state until the requested function completes.
epoll / kqueue
register for event notification
kqueue
a scalable event notification interface introduced in FreeBSD 4.1, also supported in NetBSD, OpenBSD, DragonflyBSD, and Mac OS X.
epoll
a Linux kernel system call, a scalable I/O event notification mechanism, first introduced in Linux kernel 2.5.44
epoll vs. kqueue: http://www.eecs.berkeley.edu/~sangjin/2012/12/21/epoll-vs-kqueue.html
event loop
continually process new event notification, and dispatch to event handler(callback)
asyncio
https://docs.python.org/3/library/asyncio.html
This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives
also known as “Tulip”
Python 3.4(new module)
designed in the PEP 3156
Standard event loop
Coroutine
Only Transport and Event loop layers
Layers
Application: example.py
Protocol:
ex: http, websocket(Autobahn)
understand what byte to send, and the meaning of byte to receive
Transport:
ex: TCP
know how to send and receive byte
Event loop
callback
Selector
new module in python 3.4
https://docs.python.org/3/library/selectors.html
ask for event notification(epoll or kqueue….)
autobahn
http://autobahn.ws/python/index.html
provides open-source implementations of
The WebSocket Protocol
The Web Application Messaging Protocol (WAMP)
in Python 2 and 3, running on Twisted and asyncio.
Twisted
https://twistedmatrix.com/trac/
Twisted is an event-driven networking engine written in Python and licensed under the open source.
Github - example
https://github.com/ajdavis/asyncio-chat-example
Using a virtual environment with Python 3.4(Ubuntu): https://robinwinslow.co.uk/2013/12/26/python-3-4-virtual-environment/















