#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Performance comparison: Flask vs Quart

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've done all this work and converted our web app to be asynchronous friendly and we've converted it to use Quart
0:09 which will actually execute the stuff in parallel. So here's the moment of truth did we actually get anything out of that? Was it any better at all?
0:17 The truth is it depends a lot on a lot of different conditions. Much of, under certain circumstances, absolutely so let's find out.
0:25 We're going to use this tool called wrk which as a modern HTTP benchmarking tool let's you on the command line sort of hit a URL or set or URLs
0:33 and say, test them with this many clients and this many connections and see how fast you can do requests
0:38 and see if there are any errors, things like that. So this is kind of hard to get if you're on Mac OS like I am, it's pretty easy.
0:45 You can brew install it. You can see the bottom initial link on how to install it at these various locations. So on Mac OS, brew install is pretty easy
0:53 if you're familiar with Homebrew. On Linux, you have to compile it from source. You check it out from GitHub and you compile it.
0:59 It's not too hard in Linux. And on Windows 10, you can also install it but the way you install it in Windows 10
1:06 is you install the Ubuntu subsystem for Windows 10 and then follow the Ubuntu Instructions. So pretty much, that is probably the most work.
1:15 You don't have to install us you can use any load testing tool. I'm just going to show you some results. So here is what we got when we run wrk
1:22 against our Flask version of the sun API call. So we're saying here, run with 20 threads 20 concurrent connections
1:34 and pound the heck out of this URL for 15 seconds. Run it for 15 seconds, 20 threads, 20 connections
1:42 and see what you get. Well it turns out we got not such great response. We got 4.72 requests per second. That sounds okay, but if you look over here
1:52 actually we got 71 requests per second, but see that red? We got 65 timeouts. That's bad, that's not really amazing at all to me actually.
2:04 If I had a site, and half of the time it was timing out it would be basically considered to be down. Why is it timing out?
2:11 We talked about as many, many requests come in if you can't keep up, they backup, and backup, and backup until eventually we got to the point
2:19 where those requests actually timed out. So this is the straight Flask version of that code and remember these calls take pretty long.
2:26 It's like a half a second per request to do the API interaction and stuff. How about switching to Quart and running the exact same thing?
2:34 Here it is with Quart. First of all, notice the red line is gone. The red line is gone, timeouts are gone, that's awesome.
2:41 We also, instead of having 71 requests per second we have, sorry, 71 total requests, we have 311 which is instead of four requests a second
2:50 20 requests a second so actually it's about four times better in terms of performance, so that is pretty awesome.
2:58 One word of warning if you go and do this. This is basically a deployment for Quart in general. If you want to do this testing
3:07 and you run Quart under a standard WSGI based web server uWSGI, gunicorn, something like that without configuring gunicorn in a certain way
3:19 it's still going to just act exactly like Flask. You have to run it in a different kind of web server that understands a different asynchronous API.
3:27 The guy who made Quart created a variant of Gunicorn called Hypercorn so if you wanted to take advantage of this asynchronous bit
3:35 and if you're using Quart, of course you do otherwise why would you not just use Flask? If you want to take advantage of that
3:41 you need to run with Hypercorn which we got when we installed Quart itself. So just be really careful around doing this testing
3:49 that you actually run your app in Hypercorn and not just run it like we did out of PyCharm because then you'll get Flask performance for both of them.
3:57 This isn't change the world levels of performance but all those errors are going away and much better response time.
4:03 I would say that that's actually a really big deal. Thumbs up to Quart. I definitely improved the performance here.

Talk Python's Mastodon Michael Kennedy's Mastodon