FastAPI vs. Django vs. Flask

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up this video is brought to you by lenode cloud computing if you guys are looking to host your website out there so everybody can see it i recommend you check them out they're going to save you a ton of money over azure aws they have products that pretty much suit anybody from small developers to large corporations they have data centers around the world and they're continuing to grow all the time and they also have a youtube channel that you guys can check out if you're interested in hearing more about their products directly from them hey everybody what's up all right so in this video what i want to try to do is sum up the current state of python web development in 2020 right now and what is the best web framework to use and really what is the best one when it comes to a sync and a weight so the three main web frameworks out there for python that i'll compare is django probably the largest most popular one and i've been talking about it for like a decade now and then there's flask and i've been talking about this one for a long time too everybody knows about this and you guys are probably like why am i even comparing this because there's so many videos that compare these two but then there's the new kid on the block which is fast api and this is the real one that i want to talk about in this comparison so i won't compare like all the features of all three of them or any of that stuff just because that would be a really long boring video but let's talk about fast api and what this thing brings to the table over django and flask and that is the async await uh support right so python has a sync await support going back a couple years now it looks like three seven plus is when they first added a sync and weight i could be wrong on that but the problem with the sync of weight is the global interpreter lock really you know python is an old language it goes back to 1990 back in 1990 everybody was using just one cpu core and then over the years we stopped making cpu cores faster and we spent more times making uh making mold you know multi-core processors so you could actually run a bunch of tasks simultaneously and python though wasn't designed to do something as simple as what i described and really it's because the sink awaits stuff concurrency stuff is not simple at all anybody that tells you that that's simple they've never really worked on a real like concurrent system they've never had to like find the needle in the haystack type bugs uh for just you know these runtime errors that are occurring um so i feel like anybody that says that it is simple is just they have no idea what they're talking about a sync await concurrency uh parallel processing all that stuff is is complicated and the way python was written it makes it really hard to add a synchronous support to the python code base so we use this like product to sync io but the main reason why it's such a problem is this global interpreter lock that we've always had in the python library so basically context switching is what python does when it it executes multi-threads and it has to switch between these threads so contact switching is actually storing state there's basically a bunch of overhead and even for idle processes python will still do context switching so no matter what anybody says you know there's going to be articles that say python does this or that you just have to install this or hack it this way or that way like python is not going to compete when it comes to other uh languages that have implemented a single weight a little bit better i would say like c sharp probably rust uh golang any of that stuff is is going to be a little bit easier to do concurrent programming than python so i think when it comes to really when you're picking the stack for what do you want to choose i would still use django if like i had to set this thing up beside behind a like a relational database if i were going to be building any sort of like news website like a blog i would say like even my online educational platform i would be able to do that in django although i chose node and sjs but like django is still a good option for something like that and then the same could be said for flask right flask it's big thing is like we're not django um we're very easy to use and and within like one file you could get up and running but we're not django we're not so complicated we're not so big right but then every application eventually becomes this big monolith and you end up having to tack all this stuff onto it anyway so django for the most part is including a lot of stuff you're going to need in the end large companies use both of these products i mean flask is used by netflix and django's used by instagram and you could name a bunch of big companies that are using you know both of these projects for little things you know here and there but both of the projects suffer when it comes to a zinc and a weight and i would avoid both of them if you're looking to go in that direction and that is where fast api comes in so this is a new project that is actually also built on a bunch of different projects as well like flask gets a lot of credit as a web framework but it's really built on work zug as like the primary server back-end so fast api is really no different it's built on a framework that's called starlet and this is another new python framework and this guy is built and with with this asgy module in mind and what is ascii this is where like things get even more complicated when you used to set up a python web stack like i used to use you whiskey and mod whiskey and all these different whiz keys but whiskeys are they're a web server gate they're really just a communication protocol so that python can communicate with a web server like apache so in the old days we use you whiskey mod whiskey and those are all synchronous models and starlet uses this new ascii model which django also says that it supports but the problem is django does not support the await feature so you can do something asynchronous but you can never wait for something like it it's not really like a true solution for using ascii at the moment hopefully that changes in the future but then again neither is really flask so when it comes down to it the reason why you want to use fast api which uses starlit which uses this asgy which is actually using something called uvicorn and uvicorn is it really goes as the uvicorn starlet fast api but why do we do that it's because it's faster so let's go ahead and look up like uh here's a benchmark list this is one of the most um you know really it's from tech power attack and power but they do the best job i think when it comes to listing all the different benchmarks the bottom line is that like flask and django they're down towards the bottom especially if you're communicating with a database you can see django's at 345 even lower and then if we look at something like if we look for fast api um and in here with j it's going to be quite a bit faster and then here's fast api at 228 so it's much much faster than django or flask so to sum this up this uvicorn is an ascii web server azky over a whiskey that the difference is a synchronous versus asynchronous the a and there is for a synchronous now right away with this project one of the problems with uvicorn is that you can see http 2 is still a planned support so again very much similar the way flask is built on workzook start like i'm sorry fast api is built on top of starlet so you could actually jump in and start working with starlit directly which has ascii support using uvicorn and here's an example of an app that is using starlet so this is just going to be just as simple you're defining your response to return a json data and it has json serialization as well and it also has built-in debugging support too so like you can get actual error messages and such instead of just getting a 500 error message it's going to you know spit out whatever the the stack trace is and that's really helpful when it comes to debugging so basically i'm in a virtual environment right now so i'll go ahead and spin this up so i just say uvicorn start the app it listens on localhost 8000. and here you can see when we navigate to the localhost we get our hello world back we also have all the headers and everything already written for us too so it's returning application json data there so a lot of stuff that you don't want to write if you just want a basic web server up and running starlet can also use template engines like jinja 2 and that's also pretty awesome as well so if you're a fan of django template engine which i think is the best template engine of all of them i love jinja 2 because i can use that outside of a django project but in addition to actually returning html uh json data connecting the databases or whatever all this stuff can be run a sync and a weight and then um the uh it has static file supports as well so here's like a css file in here so i can turn my h1 and i'll go ahead and just show you that so i'll just jump back over run my templates app i don't have my virtual environment running inside vs code which is why i'm switching back to my command line and i'll take a look here and we got a css template engine like i said that's how you return json data you can connect that to a database get full a sync await deploy it to an ascii web server and you can get much much faster python so if starlet can do all that stuff then why do we need fast api and the answer to that question is fast api takes starlet and does so much more so it's reliant upon starlit but it's extending it by quite a bit so you can see this is the logical progression so just a couple of those things that it does extras it has testing support but it's also using pi test for that it has a dependency to even use pi test i think it uses requests which is a popular library i have tutorials on that as far as validation for form validation or serialization then that is using pydantic so the bottom line though is that uh this will do a whole lot more so you need database drivers you need to do like course headers all this stuff like you're going to need to run it you're going to run into those problems and have to write some code eventually and you can see fast api api is basically just stating look we're just extending what starlet is not doing so another thing that fast api does is type hints and you can see here that this was added in recent support so it's very new in version 3.5 and um open or fasta i keep saying open api i'm so used to that but fast api is taking advantage of that and it's able to provide type hinting now and that it makes it a lot easier when you're dealing with linting tools and just trying to catch type errors as they occur so this is an example of adding type hinting and then now being able to see inside vs code a much more friendly intellisense using python so another reason to use fast api is that by the time you're done with your project if you're going to be building a cutting edge project project especially using a sync await you're trying to make it as fast as you possibly can maybe you're building your own cdn or just like you're gonna have massive amounts of traffic otherwise like if you don't have a bunch of traffic then why would you add all these headaches so the point is that you're probably going to be trying to build something badass and cutting edge and if there's a project template already out there that's taking all these like tools and they've already wired them all together do you want to spend a month figuring that out or you just want to download it and use it and that like that's what this provides that's what sjs provides as well for node and that's why i'm using it but um this like project template right here will save you a ton of time now it's somewhat opinionated with the view front end but like there's plenty of tutorials to show how to rip out vue and put in angular or react if you want to but that's like the least of your concerns when you're really trying to build a full stack application in python that's using a sync await the ascii uh you know web server gateway interface and then to have jwt token authentication and course headers and all that stuff then it's going to save you a lot of time so for example this is a project template that is using fast api with postgres it's got a user authentication model so you need to have users register deal with groups roles things like that super users it's got a built-in admin and you know you're either going to write that yourself or you might as well just use this all right so that's going to wrap up my video it really boils down when you're comparing the three am i looking for speed and a sink or weight if you are then you're definitely going with fast api if you need speed but a synchronous model is going to work for you i would say flask is going to be better than django if it's a basic application if you're trying to build something like the you know the new york times you need somebody that like you have writers that are going in and dealing with media and writing stuff and wizzy wigs all that crap like i would go with django especially if you're using a relational database model so is this going to be the future it probably will be python's definitely going in the async await direction it's going much slower than some of the other languages but python's still super popular so it's going to be around i think these projects are all three going to be around for a long time we're going to continue to see more sync awaits support with django one of the best things about django is that it actually moves so slowly they've always had a very good community that's been um they've always looked at backwards compatibility as being important because it always pisses developers off when there's like these small changes that kind of don't really need to be there they're just like very semantic and just meaningless but if it breaks like your code and you have to update you know 50 modules or something then you know people get pissed off right but your backwards compatibility is a big thing it's something that our javascript community knows nothing about but django definitely does all right so if you guys are trying to learn web development with me go to my website codehawk.com check out my courses i got a bunch of different courses really that start from like basic web development kind of moving up into the server side frameworks it goes full stack so you can even see me like deploying projects and stuff but um just yeah check that out i'm adding courses there all the
Info
Channel: Chris Hawkes
Views: 50,542
Rating: undefined out of 5
Keywords: chris hawkes, python, fastapi, fastapi vs django, async django, async await, type hinting, python 2020, python web framework, django, web framework, flask, python 3.9
Id: 9YBAOYQOzWs
Channel Id: undefined
Length: 14min 28sec (868 seconds)
Published: Wed Nov 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.