Slide 1

Slide 1 text

Asynchronous Web Development with Flask (or Django, Bottle, Pyramid, etc.) Miguel Grinberg @miguelgrinberg https://blog.miguelgrinberg.com

Slide 2

Slide 2 text

What Is Async? What is Asyncio? ● Async is a way to do concurrency (multiple things at once) in Python ● Other concurrency methods: Processes and Threads ● Asyncio is an async library for Python based on coroutines ● Other async libraries for Python: Twisted (also based on coroutines), Gevent, Eventlet (both based on “greenlets”)

Slide 3

Slide 3 text

Processes vs. Threads vs. Async Processes Threads Async Optimize CPU during blocking calls Yes (the OS does it) Yes (the OS does it) Yes (Python does it) Use blocking std library functions Yes Yes No Use all CPU cores Yes Yes and No No Scalability Low (ones/tens) Medium (hundreds) High (thousands+)

Slide 4

Slide 4 text

The Use Cases for Async ● High Scalability ○ Very large number of concurrent clients or requests ○ Long lived HTTP requests (long-polling, SSE, etc.) ○ WebSocket ● Platforms with no thread or process support (MicroPython boards)

Slide 5

Slide 5 text

sync async sync async net net sync async net net ssl ssl sync net ssl app a net ssl app end-to-end sync end-to-end async But I heard Asyncio is mind-blowingly fast... Not really.

Slide 6

Slide 6 text

Flask and Concurrency ✔ Processes ✔ Threads ✖ Async with Coroutines (*) ✔ Async with Greenlets (*) Quart and Sanic are good alternatives to Flask that are asyncio compatible.

Slide 7

Slide 7 text

Coroutines vs Greenlets Coroutine-based libraries from asyncio import sleep async def my_function(socket): await sleep(5) data = await socket.recv() Greenlet-based libraries from gevent import sleep def my_function(socket): sleep(5) data = socket.recv() Dates ● Asyncio (since ~2013) ● Twisted (since ~2002) Dates ● Greenlet (since ~2006) ● Eventlet (since ~2006) ● Gevent (since ~2009)

Slide 8

Slide 8 text

Greenlet Support in Web Servers ● Gevent (standalone greenlet based WSGI web server) ● Eventlet (standalone greenlet based WSGI web server) ● Gunicorn (includes Gevent and Eventlet workers) ● uWSGI (includes a Gevent worker) ● Meinheld (standalone greenlet based WSGI web server)

Slide 9

Slide 9 text

Switching to a Greenlet Server Example with Gunicorn and Gevent: Before: gunicorn -b localhost:8000 -w 4 microblog:app After: gunicorn -b localhost:8000 -w 1 -k gevent microblog:app

Slide 10

Slide 10 text

Realistic Flask Sync vs Async Performance

Slide 11

Slide 11 text

Questions?