"Flask At Scale" tutorial at PyCon 2016 in Portland

Posted by
on under

Pycon 2016 Logo

The tutorial line up for PyCon 2016 in Portland, Oregon has been announced, and I'm excited to be part of it with yet another Flask tutorial. For some odd reason, not all the class information I provided with my proposal was published on the PyCon website, so I want to give you a good overview of the material I plan to cover here, to help you decide if this tutorial is for you.

My Flask tutorial is scheduled for Saturday, May 28th from 9am to 12:20pm. PyCon tutorials have a registration fee of $150 each if purchased ahead of time, or $200 if purchased the same day at the door. Note that tutorial fees are separate and not included in the conference registration. Your tutorial registration also gives you access to lunch on the tutorial day.

If you want to have an idea of what you get from one of my Flask tutorials, here are the two that I gave in past years:


Flask Workshop: A beginner's Flask tutorial I gave at PyCon 2015


Flask By Example: An intermediate Flask tutorial I gave at PyCon 2014

This class that I'm preparing for this year's PyCon is for intermediate and advanced Flask developers looking to learn the patterns and best practices that can help scale a Flask application, both in terms of application size and load. In terms of complexity, this class would go after the two above and will be the most advanced Flask class I have ever given. The topics are divided into two main areas:

  • Large applications: How to organize code, templates and static files in a way that allows the application to grow without becoming a mess.
  • Flask under load: Different ways to deploy a Flask application so that it can handle large numbers of clients.

Below you can see the description and abstract as published on the PyCon website, plus the tentative class outline, which was not published:

Description

Do you think that because Flask is a micro-framework, it must only be good for small, toy-like web applications? Well, not at all! In this tutorial I am going to show you a few patterns and best practices that can take your Flask application to the next level.

Abstract

Can Flask scale? For many, that is the million dollar question. Unfortunately even the Flask official documentation is ambiguous about this topic. Flask is a small, unobtrusive framework that simplifies the interface between your application and the web server. Outside of that, it mostly stays out of your way, so if you need to write an application that can scale, Flask is not going to prevent it, and in fact, it will allow you to freely choose the components of your stack and not impose choices on you like big frameworks tend to do.

In this tutorial session, I will walk you through a list of typical patterns for medium and large applications written in Flask, specifically chosen to highlight scalability patterns and best practices that you will be able to transfer to your own projects. The class will be divided in two main sections, dedicated to scalability of the code (i.e. how to write large applications with Flask) and scalability of the deployed application (i.e. how to handle large numbers of clients).

Outline

  • Introduction (10 min)
  • Large applications (1 hour)
    • How to structure a large application
    • Using Blueprints to organize an application
    • Decorators as a way to simplify application code
    • Combining a web application with a REST API
    • API versioning
  • Flask under load (1 hour)
    • Asynchronous requests
    • Using a Celery job queue
    • Using multiple processes and hosts with a load balancer
    • Using coroutine frameworks such as eventlet or gevent
    • Using WebSocket for server-push and reduced latency
  • Free-form Discussion and Q&A (50 min)

I hope this additional information helps you decide if you want to attend. And by the way, I'm only starting to prepare the material for this class, so I'm happy to accept suggestions for topics I may have not included that fit the theme of the class.

I hope to see you in Portland, either at my tutorial or around the conference!

Miguel

Become a Patron!

Hello, and thank you for visiting my blog! If you enjoyed this article, please consider supporting my work on this blog on Patreon!

27 comments
  • #1 Fiit said

    Any chance to see it in video / live / replay ?
    (We do not have the opportunity to go to Portland... sadly)
    Have a nice day & good conf

  • #2 Marco said

    Hola Miguel!
    I can`t hardly wait to see this!

    Greetings from Germany!

  • #3 Miguel Grinberg said

    @Fiit: Yes, the videos of all the tutorials and talks are later posted on youtube.

  • #4 Christophe BAL said

    Thanks for your documents and videos on Flask !

  • #5 newbie said

    i have to admit this blog is a goldmine of high quality and most important practical and easy to follow examples

    writing just to say, thanks a lot !!

  • #6 Akin said

    Hi Miguel,

    I have purchased both of your videos on flask (An Introduction to Flask & Building Web APIs with Flask) as well as your book "Flask Web Development", all brilliant pieces of work, which have all been very helpful in my continuous stride, to achieve a strong understanding and ultimately, mastery of the art of software development.

    I really do wish I could attend your up coming tutorial , as this speaks to an idea for a side project i am planning to embark on.

    What are your thoughts on building a CMS system based on Flask? My thinking is to build such a system, in order to fulfill two things, 1.) helps me sharpen my skills and knowledge of both the language (Python), my web framework of choice (Flask) and most importantly, 2.) a non trivial project, which makes part of my portfolio, that i can eventually demonstrate to prospective employers.

    Your candid feedback, will be greatly appreciated.

  • #7 Miguel Grinberg said

    @Akin: Absolutely. It can be a CMS, a blog, or any other kind of non-trivial application.

  • #8 David said

    Hi Miguel. I was in this class on May 28. I've just begun working on a new project and was trying to understand some of the API code. I'm using Postman to test out the users API and I have hit a snag that I don't understand. I'm using v0.1 of the code and trying to create a user. I'm sending a POST to http://localhost:500/api/users, I have two headers set (Accept and Content-Type, both set to application/json). I'm using a raw JSON body containing nickname and password, but I'm getting a 400 error from the app. This same query passes in the tests (it's the third test in test_user). What am I doing wrong? BTW, I also tried using cURL with the same ill result.

  • #9 Miguel Grinberg said

    @David: the only reason I can think of to get a 400 from the create user endpoint is if you haven't included the required information in the body of the request, or maybe you formatted it incorrectly. Make sure you are sending valid JSON, and also that you include nickname and password fields.

  • #10 Josh said

    Hi Miguel,

    Thank you for the great post. I have a question about polling database for record changes and alternative patterns. In Flack you start a thread on first request to read from the DB every 5 seconds to see which users are offline. Can you talk about the pros and cons of that approach wrt to scaling? Are things like DB triggers to call a Flack API another possibility? Would love to hear your thoughts on the best pattern monitoring db record updates.

    Thank you!
    Josh

  • #11 Miguel Grinberg said

    @Josh: polling is not a great idea, and should be used when a non-polling solution is not available or is really expensive to build. The polling for offline users that I presented in the Flack codebase is not in the final version of the application, not sure if you noticed. This approach to finding offline users is a side effect of using HTTP requests, which are stateless. The only way to find out if a user is online or offline is to check how far ago the user sent a request. If that time is higher than some threshold, we assume the user went away. Once WebSocket is introduced (through Socket.IO in my example), each client has a dedicated connection with the server, so when the connection breaks the server knows immediately that the user is gone.

    I'm not really that familiar with database triggers, and I don't even know which databases support invoking a HTTP endpoint as a trigger. It feels like a strange solution to me, not sure I want my database server sending web requests in addition to doing whatever else the application needs it to do. The polling query in particular should not be terribly expensive if the field in question is indexed.

  • #12 Josh said

    Thanks for the reply, and I appreciate the insights. The case I have in mind is when an independent system updates the DB outside app, so all I really have to do go by is the change in the records. I think that case shows the importance of a service bus so other app can subscribe to notifications. Till then I agree indexing does make the queries pretty efficient. Lastly, I checked the latest version of flack, but still see the thread/db poll version in flack.py on each request (both v0.15 and v0.16) on master.

  • #13 Miguel Grinberg said

    @Josh: Oops, you are right, I did not remove the background task that finds offline users. In the latest versions it will not find any users, because when a user leaves the Socket.IO disconnect event triggers and marks the user offlline right away. See https://github.com/miguelgrinberg/flack/blob/master/flack/events.py#L58-L70.

    In the case of a distributed system, I agree that having a way for a service to send a signal to another is a good solution, definitely better than all services reading from the database to detect changes.

  • #14 tiwx2 said

    Hello Miguel,

    I am working through the video tutorial and I was wondering what the "push_model" function (in events.py v0.16) does and why it is needed?

    Thank you for the material and the many extension you provide us with.

  • #15 Miguel Grinberg said

    @tiwx2: Any time a model is updated, push_model sends it to all the clients to update their chat pages.

  • #16 Fisher said

    Hi Miguel,

    I appreciate your very nice blog tutorial and the book, which bring me into the nice hall of Flask, and support me to accomplish my first side project on aquiferre.cn, from China.

    I'm also walking through the video tutorial on youtube, after 30mins (far not done yet), it could be super helpful to get me to know how to well manage a bigger scale Flask app - not only split the apis, but register blueprints with url prefix. And in the part II I'm about to view the change of example on celery and socket-IO, which is being recently needed in my side project that involved Wechat (something like facebook/whatsApp) development - it could be much better use the async request based on Flask.

    Well, one more humble question - what's your opinion on the class based Celery Task?
    I mean the official documents rarely talks about it, but as a entry-level programmer - I do find out that maybe class based celery task could be easily written, tested and understand.
    However, I'm really junior that fail to make it work (the function based works well according to the tutorial), which always keep poping up the error - task type None is unregistered.

  • #17 Miguel Grinberg said

    @Fisher: I think class-based or function-based tasks are a matter of preference. The @task decorator is simply a wrapper of the class-based interface, so really in the end you are using the same functionality.

  • #18 Fisher said

    @Miguel -
    Thanks for the immediate response.
    I know exactly what you mean on the celery decorator thing. But it looks that they (celery team) don't encourage people to utilize their stuff with class based style. The consequence is - I got exception but there is not much info on stackoverflow about that.

    Compared with Flask, a flask restful api - more exactly, I can feel free to choose the function or class (Flask-restful) based style, and sufficient docs to support them.
    Just don't know why the celery is doing that - insufficient demand, maybe? Or we HAVE TO dive into the source code to learn how?

    Anyway, thanks again, man, for leading us to the friendly Flask - which clearly enlarges this group pretty much.

  • #19 Miguel Grinberg said

    @Fisher: I think it could be because a class is meant as a container for code plus data. In the case of a Celery task, there is no need to hold any data, since the input to the function comes entirely through the function arguments. You can actually make the same argument about REST APIs (and I do make it often), where a class based approach may look more organized, but technically isn't giving you much benefit, since the REST endpoints are supposed to be stateless.

  • #20 Paul Symonds said

  • #21 Miguel Grinberg said

    @Paul: Thanks. I fixed the link, they must have reorganized them at some point.

  • #22 King Kunta said

    <h1>18 What do you mean by restful api</h1>
  • #23 Miguel Grinberg said

    @King: That's an API built based on the REST architecture. See my articles on building Python APIs with Flask for an introduction to this model.

  • #24 Brendan said

    Really enjoyed your Flask book. Any chance of a new book about Flask at scale?

  • #25 Miguel Grinberg said

    @Brendan: Thanks! I'm definitely going to produce more material. I'm now working on the Mega-Tutorial for the rest of the year. Next year I'm thinking there will be some new long format tutorials/ebooks I hope. :)

Leave a Comment