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

Asynchronous Web Development With Flask

Asynchronous Web Development With Flask

A common misconception is that traditional frameworks such as Flask and Django are incompatible with asynchronous web servers. In this talk I'm going to show you how to write an asynchronous web application using Flask.

Asynchronous web servers are hot these days, but to have access to this technology in Python most people will tell you that you need to use one of the new asyncio based web frameworks. Unfortunately none of those frameworks have the robustness, extension ecosystem or developer community of more traditional options such as Flask or Django.

So here is one of the best kept secrets in the Python world: Did you know that asyncio is not the only way to implement asynchronous servers, and that some of the alternatives to asyncio are fully compatible with your favorite WSGI web frameworks, including Flask, Django and many others?

In this talk I'm going to show you how to write a fully asynchronous web application using Flask. All the techniques I'll show are also applicable to Django and most other WSGI web frameworks!

Miguel Grinberg

September 13, 2019
Tweet

More Decks by Miguel Grinberg

Other Decks in Programming

Transcript

  1. Asynchronous Web Development with Flask (or Django, Bottle, Pyramid, etc.)

    Miguel Grinberg @miguelgrinberg https://blog.miguelgrinberg.com
  2. 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”)
  3. 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+)
  4. 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)
  5. 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.
  6. Flask and Concurrency ✔ Processes ✔ Threads ✖ Async with

    Coroutines (*) ✔ Async with Greenlets (*) Quart and Sanic are good alternatives to Flask that are asyncio compatible.
  7. 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)
  8. 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)
  9. 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