Latest / Talk Python To Me / #433: Litestar: Effortlessly Build Performant APIs
Transcript
- 0:00We all know about Flask and Django, and of course, FastAPI made a huge splash when it
- 0:04came on the scene a few years ago.
- 0:06But new web frameworks are being created all the time, and they have these earlier frameworks
- 0:11to borrow from as well.
- 0:12On this episode, we dive into a new framework gaining a lot of traction called Litestar.
- 0:16Will it be the foundation of your next project?
- 0:19Join me as I get to know Litestar with its maintainers, Jacob Coffey, Janek Noviante,
- 0:24and Jacob Finchner.
- 0:26This is episode 433, recorded August 30th, 2023.
- 0:31Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:49This is your host, Michael Kennedy.
- 0:51Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using
- 0:55at Talk Python, both on fosstodon.org.
- 0:58Keep up with the show and listen to over seven years of past episodes at talkpython.fm.
- 1:03We've started streaming most of our episodes live on YouTube.
- 1:07Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified
- 1:12about upcoming shows and be part of that episode.
- 1:15This episode is brought to you by Sentry and us over at Talk Python Training.
- 1:20Please check out what we're both offering during our segments.
- 1:22It really helps support the show.
- 1:24Hey everyone, Janek, Jacob, Cody.
- 1:28Welcome to Talk Python To Me.
- 1:29Hey.
- 1:29Thanks for having us.
- 1:31Good to be here.
- 1:32Yeah, it's great to have you all here.
- 1:34Really excited to talk about one of my favorite topics, web frameworks, APIs, async, performance,
- 1:41design patterns.
- 1:42Like, let's do this.
- 1:43Let's do it.
- 1:43Cool.
- 1:45So we're going to talk about Litestar, which is somewhat new to me.
- 1:49I haven't known about it that long.
- 1:51But looking at the GitHub stars being several thousand GitHub stars and release two, it's
- 1:57definitely been going for a while.
- 1:58So it's a really cool framework that I think people will definitely be excited to learn about.
- 2:03But before we get to that, let's start with you all.
- 2:06Just a quick introduction for each of you.
- 2:08Go around the pretty bunch squares of our video here.
- 2:12Janek, you want to go first?
- 2:13Yeah.
- 2:14Yeah, sure.
- 2:14My name is Janek.
- 2:15Obviously, I'm a Python developer from Germany, currently living in Cologne.
- 2:20So I'm a bit behind the rest of the other guys when it comes to the time zones.
- 2:26I would put it a different way, Janek.
- 2:28I'd say you're living in the future.
- 2:29You're living hours ahead of time.
- 2:31You know what's already happened.
- 2:32Oh, yeah.
- 2:32That sounds much nicer.
- 2:33Yeah.
- 2:35Well, I currently work as a Python developer.
- 2:38Before I got into Python, I worked as a carpenter.
- 2:42So I built Furniture and other cool stuff.
- 2:47I think that's all we need to know right now.
- 2:50So Cody, why don't you continue?
- 2:52Yeah, I'll be happy to.
- 2:53So hey, Michael.
- 2:54Hey, guys.
- 2:55I'm Cody.
- 2:55Really, I've kind of had an interesting journey into the Python space.
- 2:59And so I'm kind of probably atypical from the rest of the team here.
- 3:03And so I've actually been a long time database guy, specifically in Oracle.
- 3:07And so lots of big, nasty data warehouses, large transaction systems,
- 3:14and building all the glue that you've got to do to make that stuff run.
- 3:17And so I guess really my intro to Python was really around DevOps,
- 3:21doing how do you make the whole environment stay running?
- 3:25How do you keep it efficient?
- 3:27And it's really focused on the database side of things.
- 3:29And so about 10 years ago, moved to Dallas from Alabama,
- 3:32originally where I'm from, and joined a small team of Oracle developers.
- 3:36And so we got acquired by one of the big four consulting firms.
- 3:39And so at that point, I shifted from development and into cloud migrations.
- 3:45So I did quite a bit of just Oracle database migrations into the various cloud providers.
- 3:50And now, just long, about six years later, I've now wound up at Google as part of the database black belt team there.
- 3:56And I still try to figure out exactly what a database black belt is.
- 4:00But a year in, really what I can tell you is that what I do is talk to all of our biggest customers
- 4:04and figure out what are the features and things that they need to make their enterprises run on Google Cloud.
- 4:10And we work with the engineers to make that happen.
- 4:12What an interesting background.
- 4:13I would say having a really good background in databases,
- 4:17and especially the DevOps side of databases,
- 4:19is a pretty unique view for building a web framework.
- 4:22A lot of people are all about,
- 4:23oh, I got to have something from my front-end code, my JavaScript I'm writing, right?
- 4:27And that's really not the same.
- 4:29Yes.
- 4:29And my career actually originally started using,
- 4:32well, I guess now they're called low-code tools,
- 4:34but we used to call them rapid application development.
- 4:37And so there's just lots of database builders where there really wasn't any actual Python
- 4:42or any Java or those types of code involved.
- 4:44It was all PLSQL.
- 4:45So I was actually happy to get involved in the Python world and move out of that space.
- 4:50Yeah.
- 4:50Excellent.
- 4:51Awesome.
- 4:51Jacob.
- 4:52Hey, I'm pretty new to being a developer.
- 4:54I spent the last four or five years in the system space,
- 4:58more on like the IT side of things,
- 5:01building systems and helping users.
- 5:04The last year, though, I've gotten to DevOps on my team at O'Reilly.
- 5:09Auto Parts, not the book people.
- 5:10Been really interesting.
- 5:12But recently I got to join this team and I'm learning a lot.
- 5:15So this is really exciting for me to be here.
- 5:18Yeah.
- 5:18Well, it's awesome to have you here.
- 5:20And like I said, a really exciting web project that I think people will appreciate.
- 5:24So let's go ahead and jump into it.
- 5:28So Litestar at Litestar.dev, effortlessly build performant APIs.
- 5:33So who wants to give us the elevator pitch here?
- 5:38The 30-second view of Litestar.
- 5:40Yannick, I think Cody should do that.
- 5:42Oh.
- 5:43Yannick, I'd like to hear what you think.
- 5:45And then I can give you about my perspective of how I kind of joined the team
- 5:49and what it means to me.
- 5:51But I think it would be helpful to hear how you think about it.
- 5:54All right.
- 5:54So what is Litestar?
- 5:56Well, I think our tagline puts it pretty well.
- 6:00We definitely have a focus on building APIs.
- 6:04So not, well, not the typical HTML applications monolith.
- 6:09It's often compared to FastAPI.
- 6:12And it has similarities, definitely, which FastAPI is already in the name.
- 6:17It's also focused on building APIs.
- 6:19But for us, really important is the effortless part.
- 6:23So what we strive to do is to take away all the, well, not all the,
- 6:28but as much as we can take away the boilerplate for developers,
- 6:33you usually have to do anyway when you're building any project of size,
- 6:39which includes lots of stuff like authorization, caching, ORM integration,
- 6:45and all these kind of things that you usually have to do.
- 6:49And with micro frameworks like Flask or FastAPI or Starlette or any of the other ones out there,
- 6:57because there are a lot of them, and they are all really great at what they're doing.
- 7:01But they do require to build a lot of boilerplates, which can be good,
- 7:07because it gives you a lot of control over what you're doing,
- 7:10and you can build it exactly how you want to.
- 7:13But it's also not, well, it's not completely effortless,
- 7:17which is what we are trying to achieve.
- 7:20Yeah, when I think about the web frameworks,
- 7:22I have this sort of bimodal distribution at like two ends.
- 7:27On one end, you have Django, where it comes with all of these helpers and all of these things.
- 7:31Like you just say, yes, I want to hold back in to manage my database with a UI, for example, right?
- 7:37But there's a bunch of those kinds of things, form creation and the ORM migrations.
- 7:42All that stuff is kind of just, you get it all.
- 7:45And the other end, you have Flask and you have FastAPI and a whole bunch of others,
- 7:49Sanic, you name it.
- 7:50And there's a lot, and they're all about,
- 7:53we're going to go and handle the request.
- 7:56And then it's up to you.
- 7:57Do you want a database?
- 7:58You can have a database.
- 7:59If you don't want one, don't have one.
- 8:01You know, in that regard, FastAPI itself is kind of almost prescriptive
- 8:05in that it talks about having like model exchange
- 8:09and defining models that create your, how the website works.
- 8:13Whereas Flask doesn't even have that.
- 8:15You just kind of nailed it.
- 8:16And this is actually how I kind of came into,
- 8:18it was actually Starlette at the time.
- 8:20But I guess about four years ago,
- 8:22I built a pretty large scale Django app as, as for some consulting work.
- 8:26And it was really around data quality and data migration.
- 8:30And it worked really well,
- 8:31but I was reading all this stuff about FastAPI.
- 8:34And I really liked what I was seeing.
- 8:36And the developer experience of that was really incredible.
- 8:38And, you know,
- 8:39the tools that those guys put together was just kind of second to none.
- 8:42It was really refreshing to see that kind of build in over what you see in Django.
- 8:48And so I started working with FastAPI and really liked it.
- 8:51But I got to where I felt like I had a lot of boilerplate that I added on top of that to get to that working app.
- 8:57And so when we joined up with Google, one of the things that I did was, was say,
- 9:01okay, I've got a lot of like boilerplate for things that I need to run this app.
- 9:05And, and maybe there's somewhere that I can contribute.
- 9:08And so at that point, I started looking around at all the web frameworks.
- 9:12And that's when I got introduced to Starlette at the time.
- 9:15And just to kind of, to give a little bit of a history,
- 9:17it was originally called Starlette because it was based on Starlette,
- 9:21just like all the other ASCII frameworks out there.
- 9:24And, you know, obviously Starlette is an awesome tool.
- 9:27And we were kind of, you know, paying respects to that by naming it Starlette.
- 9:31But obviously there's very few letters in between that and Starlette.
- 9:35And so what we found is that many of the posts that we made, people were confusing.
- 9:39Hey, did you mean Starlette?
- 9:40Because I don't know what a Starlette is.
- 9:42And so long story short, we said, okay, it's time for us to rename.
- 9:46And, you know, I guess we're all not too original because we just flipped the wording around.
- 9:50And that's how we came up with Litestar.
- 9:51It's a cool name.
- 9:53I like it.
- 9:53Yeah.
- 9:54And just for people who don't necessarily know,
- 9:56much of FastAPI's magic is that it's built on Starlette.
- 10:00And so in a sense, you're running on the same foundation as FastAPI in that regard, right?
- 10:05No, we're actually not anymore.
- 10:07So...
- 10:08Okay.
- 10:08That was the original.
- 10:09All right.
- 10:09I think we have dropped Starlette as a dependency about like six, seven months ago before version
- 10:18two.
- 10:18Yeah.
- 10:18So in the beginning, we were...
- 10:21Starlette is built very, very modular.
- 10:23You can use like FastAPI, use the whole thing, and you can just extend the router and don't care
- 10:30about anything, but it's also designed in such a way that you can just take certain things of it.
- 10:35So you can say, okay, I just like the routing and the rest I'll do myself.
- 10:40And what we originally did was we had our own router, our own routing system, and plugged that into Starlette and build our own application on top of that.
- 10:51But over time, we have diverged quite a lot from the way Starlette wanted to do things or...
- 10:59Well, not wanted to do things, but Starlette became a bit restrictive because we wanted to do things very differently at very deep parts of the Starlette stack.
- 11:09And so it kind of made sense to us that we just wrote our own basically and filled in the gaps that Starlette left behind, which wasn't an easy decision because Starlette is a very great piece of technology and it's very well done.
- 11:25And it's got a lot of credibility to it, right?
- 11:27Yeah.
- 11:28There's a lot of people that run it in production and that means something when you have a tool that is known to work well.
- 11:34It was a bit of a challenge to get that going.
- 11:36But yeah, at the moment we are from the ASGI side, our very own thing.
- 11:42We don't depend on anything else in that regard anymore.
- 11:46Okay.
- 11:46And is that all Python or has that got some other technology making it go in there?
- 11:52So that part, the ASGI part is all Python.
- 11:55We have some other non-Python parts, but they are not at the web serving site, let's say.
- 12:03So we've Rustified, I guess that's the term, if you will, at least one place.
- 12:07That's the URL parsers, right?
- 12:08The query parsers.
- 12:09Okay.
- 12:10That certainly is a strong trend these days.
- 12:12Although I'm surprised for a framework that hasn't been around that long that it's already got Rust.
- 12:16No, just kidding.
- 12:17We experimented about a year ago.
- 12:20We experimented actually with more Rust and that was to do the routing in Rust.
- 12:25So we use a Radix-based router.
- 12:29That's something Sanic does that as well.
- 12:31And we experimented with a Rust implementation, but it was decided that the speed up that we got from using Rust wasn't really worth the trade-off between it being harder to maintain and being less accessible for other contributors.
- 12:47Because most of the people who are using a Python web framework, they will know Python, but they're not necessarily that fluent in Rust.
- 12:56And, well, basically the router wasn't really that big of a bottleneck for Starlette at the time.
- 13:02So it didn't make a lot of sense to write that part in Rust.
- 13:06Yeah, it's a big trade-off, isn't it?
- 13:08I mean, even things like shipping wheels and just pushing out a version once you start to go into a, well, there's a per platform compilation step.
- 13:16That has a lot of friction, right?
- 13:18And I'm sure you could do a whole podcast just on packaging, but Python packaging in and of itself is not always the easiest or most intuitive process.
- 13:26And so, yeah, it definitely gets complicated when you add in another language.
- 13:30Yeah, I can imagine.
- 13:31And in a lot of cases, there are more low-hanging fruits that you can grab and just optimize things there before you say, okay, now we have optimized everything so well.
- 13:40The only way we can get faster if we now use a language like Rust.
- 13:44And I don't think we're at that part yet.
- 13:48So we have yet still a lot of things to do.
- 13:50That's excellent.
- 13:51It's a really good philosophy too, I think.
- 13:52There's an interesting new way to make your Python code faster that used to be the case when Moore's law was really in effect.
- 14:00You went from a 486 to a Pentium to a, you know, whatever, gigahertz and from megahertz to gigahertz and all those things.
- 14:07You just wait and the hardware got faster.
- 14:08So your code went faster.
- 14:09But with the faster CPython initiative and Quito and Mark Shannon and team over there, they're making Python quite a bit faster constantly with every release.
- 14:19And that's...
- 14:20It's really impressive what they've done.
- 14:21It was a noticeable for my, the projects that I'm currently using LightSore on, it was a noticeable increase in performance when I went to 3.11.
- 14:29And yeah, looking forward to seeing what all they do over the next couple of releases.
- 14:32Yeah.
- 14:33We just last week, I think, no, this week perhaps, had the release candidate for 3.12.
- 14:39So kind of final beside the bugs, which is, you know, people can start testing it and see what's to come out of there as well.
- 14:46Yeah, we haven't actually tested yet with 3.12 because we're still waiting on some of our dependencies to be compatible with that.
- 14:54Jacob, do you want to say something?
- 14:56We just have two more.
- 14:58I think it's GRPC and Greenlit.
- 15:01Greenlit actually, I think, is ready, but they need to do a release to PyPI.
- 15:05But the GRPC is, I think, one of our stragglers.
- 15:10Okay.
- 15:10I've been eager to test that and see what kind of performance we can get.
- 15:14I guess we could do the RC one now, but we should probably test it out at some point.
- 15:19Okay.
- 15:19Interesting.
- 15:20Yeah.
- 15:21I mean, that's always the constant struggle, right, is you've got a lot of dependencies here.
- 15:25I don't know.
- 15:26A huge number of them are optional, but yes, it can get a little crazy.
- 15:30Yeah.
- 15:30That's actually a good point to make.
- 15:32You know, one of the things you'll see is that there are quite a lot of dependencies, but you'll see that a lot of them are tied to optional groups.
- 15:38And so one of the things that we wanted to do was make it quick for a user to kind of pip install one thing and have all the pieces they need to get started.
- 15:46And so you can say pip install Lifestar, and you can add the CLI or the standard group, and it'll automatically install the JS beautifier and the command line utilities and rich and a couple of other libraries.
- 15:58And so there's a lot of helpers to kind of make that a little bit more easy to just jump right in.
- 16:03Right.
- 16:03This portion of Talk Python To Me is brought to you by Sentry.
- 16:07Is your Python application fast, or does it sometimes suffer from slowdowns and unexpected latency?
- 16:14Does this usually only happen in production?
- 16:16It's really tough to track down the problems at that point, isn't it?
- 16:20If you've looked at APM application performance monitoring products before, they may have felt out of place for software teams.
- 16:26Many of them are more focused on legacy problems made for ops and infrastructure teams to keep their infrastructure and services up and running.
- 16:34Sentry has just launched their new APM.
- 16:37New APM service.
- 16:39And Sentry's approach to application monitoring is focused on being actionable, affordable, and actually built for developers.
- 16:46Whether it's a slow running query or latent payment endpoint that's at risk of timing out and causing sales to tank, Sentry removes the complexity and does the analysis for you, surfacing the most critical performance issues so you can address them immediately.
- 16:59Most legacy APM tools focus on an ingest everything approach, resulting in high storage costs, noisy environments, and an enormous amount of telemetry data most developers will never need to analyze.
- 17:12Sentry has taken a different approach, building the most affordable APM solution in the market.
- 17:17They've removed the noise and extract the maximum value out of your performance data while passing the savings directly onto you, especially for Talk Python listeners who use the code Talk Python.
- 17:27So get started at talkpython.fm/sentry and be sure to use their code Talk Python all lowercase so you let them know that you heard about them from us.
- 17:38My thanks to Sentry for keeping this podcast going strong.
- 17:42And it looks like you've got, say, like Oracle or DuckDB or other things that maybe not everyone, or, you know, Async, PG, all those types of things that you probably only need one of those, right?
- 17:54You're probably not doing MySQL, Postgres, and Oracle.
- 17:57Maybe, but probably not.
- 17:59We actually do all of them.
- 18:00And so with the same, and this is one of the things that I think is probably good to point out, is that with the repository contrib module that we've created, you actually can use the same repository, the same models, the same JSON type, and it will automatically select the best data type for whatever engine you're running.
- 18:18So, for instance, if you're on, let's just say that today you're running on Async, PG with Postgres, and you've got a JSONB data type using the built-in custom Litestar JSON type.
- 18:28And tomorrow you convert to Oracle.
- 18:30All you need to do is change your connect string, and it'll automatically deploy that to Oracle with the correct JSON type.
- 18:36And so you really don't have to do anything additional to make your code work between that.
- 18:41So, honestly, a lot of that came from my time with Django, where you got the, you know, one set of utilities worked with quite a few databases.
- 18:48And so I spent quite a bit of time kind of making sure that that worked.
- 18:52And so you'll see that including with the Alembic migrations that are coming out in 2.1 in a couple of weeks.
- 18:59And so through the CLI, you'll be able to actually manage your entire database, call and configurations and generate them, as well as, you know, launch and use your app through the same, you know, single CLI.
- 19:10I don't think I'd ever want to migrate to Oracle.
- 19:14Well, when you need Oracle, you do need Oracle.
- 19:16I'm aware of the stigma.
- 19:18But, yeah, there's some times and places for all of them.
- 19:21I think for those who are unaware what you're talking about, you're talking about the SQLAlchemy repository patterns that we offer.
- 19:29So the things you mentioned, they are built on top of SQLAlchemy, which in itself is already really flexible and makes it easy to change databases.
- 19:38But there are still a few gaps that you need to bridge yourself, like the ones you've mentioned.
- 19:43And this is an example of the things that we try to take care of.
- 19:48One last note on that.
- 19:49So a lot of the repositories that you might see for cookie cutter apps or the other existing templates out there usually stop at the basic CRUD operations.
- 19:59But what we've done here is actually implemented all the basic CRUD operations and efficient bulk operations based on whatever database you're using.
- 20:07And we choose the most optimal method for that.
- 20:09So that includes bulk add, bulk update, a merge statement, bulk delete, as well as all of the standard CRUD operations.
- 20:16And so one of the things that we really have focused on is making sure that this is an incredibly feature complete repository that has all of the functionality that you might want to use just right out of the box.
- 20:27That's really excellent.
- 20:28They all are handling that for people.
- 20:29And it gives me a sense of what you mean by the helping people do this stuff effortlessly, bringing a little bit of those batteries included feel of Django without.
- 20:38Putting in the batteries.
- 20:39Without the very prescriptive way that, say, Django does.
- 20:43I guess keeping the micro framework feel, but bringing along a lot of the stuff that people would otherwise have to choose and configure like, oh, OK, we're going to use I guess we'll use SQLAlchemy.
- 20:52Oh, did you call an async function?
- 20:53Oh, well, then you're going to also need the async SQLite library installed.
- 20:57How do I find that?
- 20:58And, you know, like those series of steps you've kind of got to go through.
- 21:02And it sounds like you've taken care of some of that for people.
- 21:05We try to, you know, obviously there are probably will continue to evolve it over time.
- 21:08But, you know, I've now used it a year at work at Google and it's it's really kind of satisfied.
- 21:13I'd say 95 percent of the use cases I need.
- 21:16And so I typically don't have to drop back into raw SQL anymore, which I think is a huge thing.
- 21:21And that's what I would like to propose and get everybody else to.
- 21:24And so that's where the focus there.
- 21:27And the one thing I'll add about it is that we still kind of maintain that micro framework philosophy because all this work is actually packaged up in something called a plug in.
- 21:35And so you can configure this one class and it automatically registers the route handlers, the own startup, own shutdown handlers that need to happen.
- 21:44It'll register the ASCII lifespan things that you need.
- 21:48And so basically you get this one piece where you can, you know, just set up your entire app and you don't have to, you know, do do or add the piece in several parts of your application.
- 21:57Yeah, excellent.
- 21:58So before we dive into the features, which we have been doing a little bit already, at least some of the philosophy, I want to talk about benchmarks.
- 22:04And I know benchmarks are a little controversial in the sense that, well, the way I'm using the framework is different than the way you use it.
- 22:11The way you use it is really fast.
- 22:12The way I, you know, whatever, right?
- 22:13Like putting, putting that out there and just giving a sense that this is a really fast framework.
- 22:19You know, how does it compare to things like FastAPI or court, which is the async version of Flask ish, right?
- 22:25They're working on unifying those more, but basically the async version of Flask for now.
- 22:29Sienek and then Starla, who wants to give us a quick summary of this graph you got here in the benchmarks page?
- 22:35Before we get into the benchmarks, you said it already, but I want to add another disclaimer.
- 22:40Like, as you said, benchmarks are really, really controversial topic and they're insanely hard to get right.
- 22:47And it's even harder to get actually benchmarks that are useful for your use case.
- 22:53And they show you what you actually want to measure because most of the time they don't.
- 23:00They measure something, but they often don't translate one to one or even somewhere close to that to real world performance.
- 23:09And I have spent a lot of time on these benchmarks.
- 23:12And I want to say that the benchmarks didn't came about as us trying to compare to other frameworks,
- 23:19but they were experiencing some performance regression internally after a major change somewhere.
- 23:26And we were trying to track that down.
- 23:28And for that, I developed a quite comprehensive benchmark suit that tried to get us close to a real world usage of how we expected the framework to be used.
- 23:39And then that grew to compare other frameworks as well.
- 23:43And when I added the other frameworks, I tried to follow a very, very simple philosophy, which is not necessarily where some might say it's unfair.
- 23:53I think it's one way to get a comparable result.
- 23:57What I tried to do is to not optimize anything.
- 24:01I just used every, I built the same app in every framework with the framework as it comes out of the box,
- 24:08just took the straight up approach that's shown in the documentation.
- 24:12And I did that because from almost all of the frameworks, there is for every case,
- 24:17some way to make it a little bit more performant in this special case and in that special case.
- 24:23And I'm not an expert in all of these frameworks.
- 24:26And I'm sure if you start optimizing, there's no point where you can say, okay, now it's completely optimized.
- 24:33So I just took the completely opposite approach and didn't optimize anything at all.
- 24:38And that includes Litestar.
- 24:39We also do things that could be made more performant in Litestar, but we don't do them in the benchmarks.
- 24:45Well, that's just our bench line of what we are comparing to.
- 24:49And I just think it's an important context to have.
- 24:52Yeah, that seems fair.
- 24:53So we know what we are comparing.
- 24:54Right.
- 24:55Okay.
- 24:55So if we look at the benchmarks, one thing we can see there.
- 24:59So we have the synchronous and asynchronous performance.
- 25:02And one thing that we can see there is that for Litestar, it's almost identical compared to, for example, Starlet, where it's not.
- 25:10The reason for that is our model of execution for synchronous operations.
- 25:16What Starlette does is that for the synchronous function and asynchronous framework and it's blocking, you might potentially block your main thread and all other requests that are coming in at the same time.
- 25:34You don't want that.
- 25:36So definitely the safest option is to just put that in a thread pool, let it run and you're good.
- 25:42You're good.
- 25:42The thing is, threads are slower than asyncIO.
- 25:46And so what we do is we force our users to make a deliberate choice when they have a synchronous function.
- 25:54So we say, do you want to run that synchronous function in a thread pool or not?
- 26:00And if not, we just don't do that.
- 26:02Is that done by a parameter to the, you know, like at get or something and then you say thread pool, yes or no or something like that?
- 26:09You can set that as a parameter.
- 26:10And if you don't and you use a synchronous function, you get a very nasty warning, warning you about that.
- 26:17You can shut that off globally because some, yeah, that.
- 26:21Sync to thread equals false.
- 26:24Okay, cool.
- 26:24Yeah.
- 26:25You can shut that off globally if you don't want to be warned about it.
- 26:28But so we made the decision that it should be a deliberate choice if you want that behavior or not, because in many cases you don't actually need that behavior because you're not doing any blocking IO
- 26:40operations or any other blocking or CPU bound operations or whatever.
- 26:44So the, in fact, the synchronous functions are as blocking as the other async functions.
- 26:50So there's no benefit to be had from running in a thread.
- 26:54Yeah.
- 26:55And also a lot of times in production, the production server like Gunicorn or whatever is already using multiple threads or things to deal with that.
- 27:05And when, or at least multiple processes.
- 27:08And then when you're talking to things like a database or something, you're doing a network call, which deep down is probably releasing the GIL while it's waiting anyway.
- 27:16Right.
- 27:17There's a lot of subtles, subtleties that are, that are happening down there that maybe you don't want to juggle.
- 27:22Right.
- 27:22One point to add to that is that the sync to thread option applies to the dependency injection as well.
- 27:27And so it's not just the routes that you can add that flag to.
- 27:30And so to your point about databases, all those pieces can, can have that same kind of behavior.
- 27:36There's a benchmark for that as well somewhere that shows the same difference for the dependency stuff.
- 27:41Okay.
- 27:42I think.
- 27:42Yeah.
- 27:43So that's one difference and another difference.
- 27:46And so just to add, this is one choice that started makes for you and by extension, FastAPI as well makes for you that you can't easily turn off.
- 27:56But if you look, for example, at the sonic example, you see that it doesn't suffer from the same problem.
- 28:02So it's, you can attribute that to that, to this decision.
- 28:06The other big difference is because what we're looking at here is serializing a dictionary into a list of dictionaries into JSON.
- 28:15And one of the reasons why Litestar is so much faster in this than FastAPI, for example, is because we use msgspec, which is a JSON validation and parsing library.
- 28:30Well, not just JSON, it's also for message pack, which is an insane thing.
- 28:37It's an insanely great piece of technology, which we have been using, I think, for almost a year now when we started to introduce it.
- 28:46Yeah, that one.
- 28:48And it's super fast.
- 28:50It's written in C.
- 28:51The code can be a bit hard to get into because it's like one massive 12,000 line C file.
- 28:57So if you're not very familiar with C and the Python C API, it's not going to be an easy read.
- 29:03Yeah, but it's insanely fast and it supports a lot of things out of the box.
- 29:08So, for example, well, JSON, so all built in Python data types, but it also supports data classes and typedicts, which helps us a lot.
- 29:18And FastAPI, on the other hand, by default, well, it uses, for one, uses a standard library JSON module, which isn't as fast as any of the other external, well, not external, third-party JSON libraries that you can have.
- 29:34And it also uses Pydantic to validate the data, which I have to point out is something that we do not by default.
- 29:43So that's the reason why there's such a big difference.
- 29:46And even after Pydantic 2 has been released, which has been rewritten in Rust and has had a significant gain in performance.
- 29:58Yes, Samuel Colvin says something like 22 times faster, which is remarkable.
- 30:03Yeah, but still, if you just don't do that step at all, it's obviously going to be faster.
- 30:09Yeah, that's true.
- 30:10Can you, do you remember this graph here, whether this is FastAPI based on Pydantic 1 or 2?
- 30:16This is Pydantic 2.
- 30:18Okay.
- 30:18You could see that it's noticeably faster now with Pydantic 2.
- 30:23So there has been a huge gain.
- 30:24And to be fair to Pydantic and FastAPI, mostly FastAPI, you could also use FastAPI's ORJSON response, which uses ORJSON to serialize that.
- 30:37And it would be a lot faster.
- 30:39But as I said earlier, that would, to me, fall into the category of optimization.
- 30:45You could do similar things for Litestar.
- 30:47And what we wanted to compare is performance out of the box.
- 30:50And this is what you, what you get.
- 30:53Talk Python To Me is partially supported by our training courses.
- 30:56Python's async and parallel programming support is highly underrated.
- 31:01Have you shied away from the amazing new async and await keywords because you've heard it's way too complicated or that it's just not worth the effort?
- 31:09With the right workloads, a hundred times speed up is totally possible with minor changes to your code.
- 31:15But you do need to understand the internals.
- 31:17And that's why our course, Async Techniques and Examples in Python, show you how to write async code successfully as well as how it works.
- 31:25Get started with async and await today with our course at talkpython.fm/async.
- 31:32That's some of the stuff you're talking about, right?
- 31:33You could include new JSON parsers.
- 31:35You could include uvloop, for example, and there's a lot of optimizations, right?
- 31:39The benchmarks are on uvloop.
- 31:41I think that's one optimization we did across the board for everybody.
- 31:44Everyone uses a single uvicorn worker.
- 31:47Yes.
- 31:48So the environment is the same for all frameworks that we test.
- 31:52It's uvicorn with uvloop, the siphon dependencies, and one worker pinned to one CPU core that's shielded.
- 32:01So it just sort of gets something comparable.
- 32:04And that's awesome, actually.
- 32:05That's really cool.
- 32:06I like it.
- 32:06And I guess I'd just like to point out, though, that often there's other things that are in your bottleneck in your application, right?
- 32:12And so obviously benchmarks, take them with a grain of salt.
- 32:15And the other thing is that msgspec is awesome, but it's not as feature complete as something like Pedantic, which is really great.
- 32:25So I think there's some differences there.
- 32:28And so we wanted to make sure that you have the ability to use both.
- 32:30But in the context of benchmarks, you know, sometimes I guess it's worth noting that Pedantic is probably doing more or can do more than msgspec.
- 32:38But I don't think it's necessarily always going to be what you see here, the serialization piece that's going to be your slowest part.
- 32:44I agree.
- 32:44As a database guy, you might have database indexes and the lack thereof coming to mind or something, right?
- 32:50Well, that's one of the things, right?
- 32:51You know, and you kind of touched on it.
- 32:53It's the network latency and those kind of things between that's really going to consume quite a bit of the time.
- 32:59And I think we do have a benchmark, which is serialization of complex objects like Pedantic models or data classes or something like that, which actually I think is very interesting because it shows that if you're using Pedantic with Litestar, it's actually not faster than FastAPI.
- 33:18Because then what you're measuring is the speed of Pedantic, which in both cases is the same.
- 33:23And you can, it sounds like, which is interesting.
- 33:26Okay, so quick takeaway.
- 33:27Litestar is quite fast.
- 33:29One of the reasons you might choose it is the speed.
- 33:31And it sounds like there's a lot of good options there.
- 33:34All right.
- 33:34But not the only one.
- 33:35On that note, if you allow me to point out one more thing.
- 33:38Of course.
- 33:39We are quite fast.
- 33:40And I think for the feature set that we have, we are probably among the fastest, but we are not by far the fastest ASGIF framework out there.
- 33:50That would be, to my knowledge, black sheep.
- 33:52Black sheep.
- 33:53Which is insanely fast.
- 33:55And we actually don't include that in the benchmarks because it makes the benchmarks absolutely useless.
- 34:00Because then you just have one gigantic bar that's black sheep.
- 34:04And then you have two very, very teeny tiny bars, which is everything else, which is another micro framework that's written in Cython.
- 34:12I have not heard of black sheep.
- 34:14That's something I have to look into.
- 34:15Okay.
- 34:16Cool.
- 34:16But obviously, speed is interesting.
- 34:19Speed is important.
- 34:19It certainly is something that if it was really poor, people might choose like, well, it's interesting, but it's not that fast.
- 34:25But given the speed, it's certainly an advantage, not a drawback.
- 34:30But I think a lot of the advantages come from a bunch of the features.
- 34:34So maybe we could talk through some of these and whoever wants to just jump in on them as we go, feel free to.
- 34:39So I think it probably came through already from the conversation.
- 34:42But the programming API is very micro framework, Flask, FastAPI like, right?
- 34:47You create a function, you do an at get, give it a URL, decorator on the front, and you've got an endpoint on the web that you can do things with.
- 34:57So that's pretty straightforward.
- 34:58At its core, exactly.
- 34:59And we take it one step further.
- 35:01So, you know, all of the patterns you know and love from Django.
- 35:04So some of the things that you see from Django REST framework.
- 35:07So we have controllers that are very similar to that, where you can define a class and have multiple methods in it.
- 35:12And so, you know, that's really kind of where things start to differentiate.
- 35:15But at its core, we definitely wanted to make sure you had that exact micro framework experience that you see everywhere.
- 35:21So the first one, let's just touch on some of the main features here.
- 35:24The first one is data validation and parsing.
- 35:27So leveraging the power of type-ins, which is very, very nice.
- 35:30Who wants to highlight that feature?
- 35:32I think you're on mute.
- 35:33Good that you pointed that out.
- 35:34So that's definitely one of the areas that was directly inspired by FastAPI.
- 35:40Because FastAPI, a few years ago, came up with this brilliant idea of the combination of just levering the type-ins and the emerging Pydantic stuff and whatever.
- 35:52And build your APIs on that, based around that as your core.
- 35:57And it's been very increasingly popular to build your APIs like that.
- 36:03So it's definitely directly inspired and influenced by this.
- 36:06We are approaching things a bit differently, though.
- 36:09So, for example, you are not tied to Pydantic.
- 36:12You can use any data modeling, not any, but a lot of data modeling libraries that you might want to choose are supported out of the box.
- 36:21Pydantic is supported.
- 36:22You can also use msgspec, which supports some data modeling like Pydantic, not as featureful, but very, very fast.
- 36:32You can use attrs.
- 36:34You can use plain data classes or type dicts to validate your data and to transform your data,
- 36:42which is what you are currently looking at, which are our DTOs, which have been written by the brilliant Peter Schutt, who isn't here with us today,
- 36:53which are, well, data transfer objects.
- 36:56So they are a way for you to define how your data should be transformed on the way in or on the way out.
- 37:03So you have incoming data that's unstructured, JSON data, and you have a target model, and you might want to apply certain transformations to that,
- 37:13say, rename fields from snake case to camel case.
- 37:16Very common thing to do.
- 37:18Right.
- 37:18While you are validating it on the flyer that it confirms to a certain schema, for example, a Pydantic model or a data class.
- 37:27And so DDOs are basically an abstraction layer between that, where you can say, okay, this is my source model.
- 37:35This is my Pydantic model.
- 37:36And it has a user ID that's an integer and it has a name that's a string.
- 37:41And by default, Litestar, if you give it that, will validate that the incoming data confirms to that schema.
- 37:48We'll have Pydantic run all the validation and parsing on it like you would normally, which is quite similar to how FastAPI does it,
- 37:57or how you might also want to do it by hand.
- 38:01The DTOs come in where you have one data model that has different representations.
- 38:07So, for example, you might have a database model that's a SQLAlchemy model.
- 38:11But on the way out, you don't want to include the password field because of course reasons, but you want it on the way in when you create the user to sign up.
- 38:22So one way to do that manually would be if you're using Pydantic to create two models, one for the way in, one for the way out,
- 38:31or to create one base model with all the properties that are the same and then two additional models, whatever.
- 38:37DTOs basically do that, but they do it for you.
- 38:41So you don't have to actually write out those two models.
- 38:44They can take in one of the models, one of the supported model types.
- 38:49I think at the moment we support Pydantic, SQLAlchemy, msgspec, Adders, and Data Classes.
- 38:56Correct me if I'm wrong.
- 38:57No, I think you got all of them.
- 38:58So if you have a class of that type, you can create DTO from it.
- 39:03And then you have a DTO config where you can say, exclude these fields or only include these fields and rename these fields.
- 39:11And all you have to do is create a type annotation with that DTO.
- 39:16And Litestar will take it, use it to transform your data, and then give your back your original model in the form you specified.
- 39:25I see.
- 39:26And you say in the Decorator, you said the DTO model that does that conversion for you.
- 39:31Got it.
- 39:32Yeah, that's a good point.
- 39:33You said it in the Decorator and not at the point where you receive or return the data.
- 39:37So the data you receive and return will always be the actual model that you're dealing with,
- 39:43which has the great benefit that your type annotations are always correct.
- 39:47And you don't have to worry about that, about, you know, casting something to something else or doing the civilization in your route handle directly,
- 39:55because otherwise the type annotations for the return type won't match because you have excluded the field or whatever.
- 40:01So you just set it completely separately from that, just as information for Litestar to say,
- 40:07okay, use this to do the transformations.
- 40:10But the end result is my original model, whatever you want it to be.
- 40:15Okay, so this is kind of the model equivalent of FastAPI, the DTO.
- 40:21That's really neat.
- 40:22There's a lot of, maybe a little bit of overlap in something like SQL model, right, where you can declare your SQLAlchemy model as a Pydantic model.
- 40:29And in this case, we, and you're welcome to use SQL model with Litestar.
- 40:34But in this case, you can now just use the normal SQLAlchemy model and declare a DTO,
- 40:38and it'll automatically convert that to, you know, a msgspec struct on the way out and serialize it that way.
- 40:45Very cool.
- 40:45That's a good point.
- 40:46You bring up the msgspec struct.
- 40:48So that's one other area where we use msgspec for to create these models,
- 40:54because msgspec is extremely fast and has this struct type,
- 40:58which is sort of like an address class or a Pydantic model.
- 41:02But it has the benefit of being, as far as I know,
- 41:06the fastest library for that type of stuff for Python that exists at the moment.
- 41:12So what we are building there, the transformation layer is as performant as it can be.
- 41:18Excellent.
- 41:18In fact, I think, and I have to go look up the actual quote,
- 41:21but I think the struct is actually faster than the data class in a lot of scenarios.
- 41:27And so they've done an incredible job with that library.
- 41:29That is incredible, actually.
- 41:30And you're beating the building stuff, right?
- 41:32That's cool.
- 41:33All right.
- 41:33We talked a little bit about the open ecosystem, right?
- 41:36The ability is pedantic versus other, you know, custom DTOs, other libraries.
- 41:42OpenAPI, Swagger, the whole generate your documentation for you.
- 41:47That sounds pretty excellent.
- 41:48I'm guessing it's based a little bit on the DTOs as well to describe the schema.
- 41:52Every class has the ability to export what that output looks like.
- 41:56And so the DTO knows how to output its signature so that it can generate the correct open API schema.
- 42:03And I guess really the main thing to point out, and, you know, we obviously do the typical Swagger schemas,
- 42:08but one other thing that we add in is Redock and Stoplight elements as well.
- 42:13And so you've got a couple of options for your documentation host.
- 42:17Middleware.
- 42:18So middleware is things that can see the request before your view function runs or make changes after it runs for cores or logging or other things.
- 42:29Do you want to talk about that?
- 42:30Cody, do you want to talk about the compression stuff, for example?
- 42:33I'll happily do that.
- 42:34So, you know, you kind of nailed what the core of the middleware is, but really it's all those pieces that you need to add in to maybe add in security or add in some type of additional functionality, compression, for instance.
- 42:47And so a lot of, you know, outside of the plugin system, a lot of that functionality is included in the middleware.
- 42:53And so you'll see built-in stuff for most of the things that you're going to want to do out of a normal application.
- 42:59There's probably a few things that you may need to roll on your own, but we've got all the core things.
- 43:04And so you've got compression, both Brotley and GZIP.
- 43:07You've got open telemetry and Prometheus integration.
- 43:11You've got several different types of authentication backends that would get integrated here, including a session-based backend that, one, there's a cookie-based backend and a session-based backend where it stores, you know, on the actual server itself.
- 43:25And we also have a JWT auth configuration that you can use here.
- 43:29So I encourage all of the listeners to just check out what we have as part of the default middleware.
- 43:34But, you know, most of the things that they're going to want to do from a web app are going to be built right in.
- 43:40We also have the logging, the cores, all the basic stuff.
- 43:44See it like cross-site reference, rate limiting.
- 43:47Cross-site, yeah, requests forgery for forums, yeah.
- 43:50And then you can add your own, and they're all just little ASGI apps that you can plug in as you need them.
- 43:55On before and on after a request, something like that, right?
- 43:58This is really cool here where you could say in a particular view decorator, you can say, for example, exclude from CSRF for just this form, for example.
- 44:08And actually, this is something that you'll see as a feature.
- 44:11You're going to see this all throughout the code, and it's layered permissions.
- 44:15And so this excludes from CSRF and several other things.
- 44:20It's hard to say.
- 44:20You may see that.
- 44:20It is a mouthful.
- 44:21You're going to see that in several different places, right?
- 44:24And so you can apply that at the controller, or you can apply that at the route level that you see here.
- 44:30And so that's one of the helpful features that you'll see where you can put it in one spot, and it'll cascade down.
- 44:37Yeah, I saw that for the DTOs.
- 44:38Yeah.
- 44:38We have a lot of that, like, layered dependency stuff, like, well, dependency, but dependency injection, like you could also do at the app level or just at the controller level.
- 44:47There's so much other.
- 44:49On applications, controllers, routers, and the route handles, these are our basic layers.
- 44:54And most of these types of configurations, so middleware's dependencies, header configurations, middleware configurations, they're all layered.
- 45:04So you can apply them on every layer you want, and they will affect the layers below that.
- 45:10So it's quite flexible how you want to or where you want to configure your stuff.
- 45:16Well, the ORM integration, we talked a little bit about SQLAlchemy as well, so that's pretty cool.
- 45:21And I'll be happy to elaborate on that.
- 45:23But, you know, we've covered quite a bit of what you'll see here.
- 45:27I think the only thing that I haven't mentioned that we've integrated in and that will be coming in 2.1 is the use of Lambda statement.
- 45:34And I'm not sure if you're even have seen that or if your listeners have, but it's a relatively new function that's in SQLAlchemy to help with statement caching.
- 45:44So the repository's been converted over that.
- 45:46Actually, I see some great, great things in the chat.
- 45:48There's HTMX integration.
- 45:50Obviously, you want to make sure we touch on that.
- 45:52And I really want to let somebody talk about the WebSockets and the channels integration, too.
- 45:58So there's some really cool stuff that I'd love for your listeners to hear.
- 46:01Before we move on to the WebSockets, which I also want to talk about, I do want to give, since we're on the ORM integration, I can see some comments out there from, for example, Roman behind Beanie, which is a MongoDB ORM or ODM.
- 46:16It says, I like the DTO concept.
- 46:18Having such tools separately would be great.
- 46:20Having things such as SQLAlchemy objects when needed, Pandas data frames, PyDenic model, depending on the context, is really cool.
- 46:27We actually have talked about that.
- 46:29Well, not we, the people present here, but Peter Schutt, the person who created the DTO implementation at me, we have actually talked about that, making the DTOs a separate library, because it's a very useful concept.
- 46:45So it's not something we have planned.
- 46:47It's something that has crossed our minds as well.
- 46:50That's very cool.
- 46:51The question was, what about the MongoDB people or the other NoSQL folks for whom SQLAlchemy doesn't necessarily want to talk to because it's relational?
- 47:02What's the story there?
- 47:03Like, is it still pretty easy to use Litestar?
- 47:05It is.
- 47:06And I think that there's actually a native integration that's maybe not totally finished, but there is an open PR for a Mongo-based repository.
- 47:13So there's going to be that much tighter coupling coming soon for those that want to use it.
- 47:19But if they, there's nothing that would limit compatibility now.
- 47:22So if they want to go ahead and configure that with your application, you're certainly free to do so.
- 47:26But there will be a first-party kind of clean integration for those things coming soon.
- 47:30Oh, that's excellent.
- 47:31So for right now, you know, B&E is based on Pydantic.
- 47:34You all work with Pydantic.
- 47:36It sounds like, can you just use that as the go-between maybe?
- 47:39Absolutely.
- 47:40And you're free to use Pydantic with Litestar just as you, as you can with, with FastAPI.
- 47:45It'll just work.
- 47:45And so there's no, there's no reason to change everything to msgspec.
- 47:50If you want to, you can mix and match and leave everything in Pydantic if that's what you prefer as well.
- 47:54That's a good point.
- 47:55So the thing we do, all these integrations with Pydantic and Eris and whatever.
- 47:59So they are not, they are not baked in somewhere deep into the application.
- 48:04They are all plugins.
- 48:06Plugins that you could write yourself if you wanted to and write for every library that you desire.
- 48:13That's one of the larger things that we tackled with the 2.0 release, where we tried to decouple us from Pydantic because we were based on Pydantic before and we wanted to be more open.
- 48:25So we basically ripped out everything Pydantic in Litestar's core and put it into a plugin and at the same time made sure that the plugin API was so versatile that it could support all the features that we had supported before.
- 48:40And now we're at the point where it's very trivial actually to have support for a library like Pydantic with everything from DTOs to open API to serialization, validation, parsing, fully supported by a fairly trivial plugin that you have to add.
- 48:59So even if it's not provided out of the box, it's fairly easy to just do it yourself.
- 49:06It's really cool.
- 49:07All right.
- 49:07WebSockets.
- 49:08Let's talk about WebSockets.
- 49:09Here's a little bit.
- 49:10Yannick was the mastermind behind this.
- 49:12All right, Yannick.
- 49:13First, tell people what WebSockets are and why they care about them.
- 49:16Why is this not just another ACTP request?
- 49:18WebSockets.
- 49:19Explaining WebSockets in a few sentences.
- 49:22Yeah, you have three sentences.
- 49:24Go.
- 49:24So for people who are around, have been around longer in the web development space, they might remember long polling.
- 49:33So where you had.
- 49:35Yes.
- 49:35Where you faked.
- 49:36Well, back and forth communication between the server and the client by having a request that never terminates.
- 49:42And you can always send more data from the server because the request wasn't actually done yet.
- 49:48And I would say WebSockets is kind of like that concept but evolved.
- 49:52So you can easily send bidirectional data from the server to the client with a very, very minimal overhead.
- 50:01And WebSockets are a core functionality of ASGI.
- 50:05You could, there were several ways you could do WebSockets with WSGI, but they were all not very easy and straightforward because they are asynchronous by nature.
- 50:17This is what they are, they are asynchronous communication channels.
- 50:20So making that into a synchronous protocol is always a bit tricky.
- 50:25And I think there's no ASGI framework that I know of that does not support WebSockets in some way.
- 50:32So it is a core functionality of that type of Python framework, I would say.
- 50:38And so our WebSocket implementation has kind of like two layers.
- 50:43You have the low layer where it's basically you receive the socket object, which is just the connection.
- 50:51And then you can act on that connection and you have to accept it and you can terminate it and you can send data or whatever.
- 50:57And then you have what we call WebSocket listeners, which are an abstraction over WebSockets.
- 51:04And they basically work like you would normally define a route handle in Flask or FastAPI or Litestar, where you receive some data and then you return some data.
- 51:16And that is your function and the rest will be handled by the listener.
- 51:21So you define a handler function for an event that might occur.
- 51:25One of the cool aspects of this is that these support all the features that Litestar supports in other layers of the application.
- 51:32So you can use DTOs with them.
- 51:34You can use validation with them.
- 51:36So if you define a DTO and you say, OK, so this is my model and this is what I want to receive, the incoming data from the WebSocket will be parsed as this model.
- 51:45It will be validated and then it will be presented to your function.
- 51:49So functionally, these WebSocket listeners, they look and work exactly the same as a regular HTTP route.
- 51:58Yeah, that's really cool.
- 51:59Enables another thing that a lot of ACI frameworks don't have, which is handling of WebSockets synchronously, because we do the async stuff in the background.
- 52:09So you can use an asynchronous function, but you can also just use a synchronous function because all the dealing with the actual WebSocket itself is handled somewhere else.
- 52:21So my thought was probably this is what I want to write in a standard like I want to receive a message from the client back to the server or and process that there.
- 52:32But if you want to do like all the weird multicast stuff, different listeners and groups of listeners you can do with WebSockets, that's probably the lower level version you're talking about.
- 52:42Right.
- 52:42Did I get that right?
- 52:44Perhaps not.
- 52:45It depends how weird you want to get.
- 52:47So a fairly standard use case would be for something like, let's say, a chat room where you have some sort of predefined channels and then you have multiple clients that want to send data over the same channel and then fan it out to all the other clients.
- 53:03And for stuff like that, we actually have a full integration, which we call channels, which themselves aren't necessarily tied to WebSockets.
- 53:13They are basically distributed message bus sort of thing.
- 53:18I have yet to come up with a good short description of what channels actually are.
- 53:24It changes a bit from depending what I'm talking about.
- 53:26I think message bus is the way to think about it.
- 53:29Right.
- 53:30And it keeps a history of the events.
- 53:32So a message bus is perfectly fine.
- 53:34They are backed by at the moment by Redis.
- 53:36We have different methods.
- 53:38So you can use PubSub or other methods that are quite a bit more involved.
- 53:43Yeah.
- 53:43And they can also handle WebSockets for you.
- 53:47So you can say, OK, so I have I want to create a channel named chat.
- 53:52And every time someone signs up to that, please accept the WebSocket request and then add the client to this subscriber list.
- 54:01And then every time a message comes in, I want to do this.
- 54:06And then I also want to distribute the message they send to all these other clients in those channels.
- 54:12And I know if they send a special message, then I want to unsubscribe them.
- 54:17So for this kind of standard use case, we have that built in.
- 54:22You can, of course, build your own logic on top of that.
- 54:25And as you said, if you want to go into really weird stuff, you will have to use the low level interface, which is also there, which you can also access from the WebSocket listener.
- 54:36So you can, by a dependency injection, just receive the raw socket object if you want to deal with that for some reason.
- 54:43So it's available if you need it, but you can do the easy thing by default.
- 54:47Oh, awesome.
- 54:47That sounds really cool.
- 54:48And the channels sound great.
- 54:49Also, Chris out in the audience said, does that also mean server-side events are?
- 54:53Yes.
- 54:54Server-sent events, rather, excuse me, are available.
- 54:56And I saw that you have a dependency on HTTPX SSE, which is like a lightweight, lightweight WebSocket type of thing.
- 55:04It's very cool.
- 55:05That's a development dependency for testing.
- 55:07Ah, for testing.
- 55:08Okay.
- 55:08Yeah.
- 55:08So we do have server-sent events support built in.
- 55:13You can do that.
- 55:13And you can, for example, use that in combination with channels.
- 55:17So instead of fanning out the messages via WebSockets, you could do that with a server-sent event as well.
- 55:24Excellent.
- 55:24All right.
- 55:25We're getting quite short on time.
- 55:27I want to close this out with maybe what would be the last thing in a process of building out an app in Litestar.
- 55:34And that would be deployment.
- 55:35And I didn't see a lot of conversation about how I should be deploying this stuff on the website.
- 55:41But I saw in the benchmarks that you said, we use Gunicorn with the uvicorn worker and so on.
- 55:47And it sounds like maybe using Gunicorn is a good option.
- 55:51What's the deployment story?
- 55:52What do you tell people that want to put this in some real scaled-out production story?
- 55:56That's still a good option.
- 55:58But personally, I think Cody as well.
- 56:00We've just been using uvicorn.
- 56:03No, no.
- 56:04Not as a worker, just by itself.
- 56:06That has worked really well.
- 56:08It really just depends on how you're going to run it, right?
- 56:09So if you're using Docker or Kubernetes or something else that's managing that process, then it's possible that Gunicorn may not be something that really is needed in your environment, right?
- 56:22And in fact, it might actually just add overhead.
- 56:24And so if you've got something like Cloud Run or Docker or Kubernetes or anything like that, what we've realized is that sometimes it's quite a bit faster to just run it with uvicorn.
- 56:32And if the process dies, then your container management, whatever you're using to manage those things, will automatically start and scale those processes out.
- 56:41It notices that the container exited anyway, and so it's going to create a new one, right?
- 56:45Okay.
- 56:46Got it.
- 56:46Correct.
- 56:46Yeah.
- 56:47But if I'm going to a Linux machine directly?
- 56:49Then I think there's more of a decision to make on whether or not you want to host it through something like Gunicorn.
- 56:55And I guess the other thing I'd like to add is that really there's any of the ASGI servers can run Litestar.
- 57:02So you've got what Daphne, Hypercorn, there's work that we're doing right now with Succotify.
- 57:07It's a mouthful as well, but they've got some cool stuff going on there.
- 57:11And so hopefully we'll have compatibility with that soon.
- 57:13And so, yeah, the idea is that the same way that you would host any other ASGI app would apply to how you would manage a Litestar app.
- 57:20Excellent.
- 57:21Then maybe Nginx or something like that in front for SSL and Let's Encrypt and all those things.
- 57:26Absolutely.
- 57:26I think that's probably about it for time.
- 57:29Final thing here.
- 57:30So you all said you just released a week ago or so, last week, release version two.
- 57:35Want to give a quick shout out to the changes for people maybe already using it?
- 57:39I don't know if that's even possible.
- 57:41This has been in development for over seven months now.
- 57:44Okay.
- 57:44And there have been substantial changes to basically every part of the application that you could think of.
- 57:52A lot of the features that we have talked about today, they are new in version 2.0.
- 57:57The DTOs, they are new in 2.0.
- 58:00The SQLAlchemy integration is new.
- 58:02Channels are new.
- 58:02The WebSocket listeners are new.
- 58:04Message spec integration is new.
- 58:06Pedantic billing being optional is new.
- 58:08So I think it doesn't make a lot of sense to compare it to version 1.0 in this context.
- 58:13So just strongly encourage people to use 2.0.
- 58:16Yeah, definitely.
- 58:17Please use version 2.0.
- 58:19We also have the new stores interface.
- 58:21As well.
- 58:22And so many other features that I forgot about or don't have time to list.
- 58:28HTMX requests.
- 58:29All sorts of good stuff.
- 58:30Okay.
- 58:31Yeah, there have been so many people, amazing contributors over the last several months,
- 58:36spending time on this and delivering awesome features for us, for the community.
- 58:42There's been so much work going into this.
- 58:44And well, I was relieved when I finally was able to hit the publish button on GitHub and we could finally get it out.
- 58:54That's a big, big project.
- 58:55That's right.
- 58:56A lot of people have already been using version 2 in production.
- 58:59Cody, you have Jacob, you have as well.
- 59:01Basically, since we started development, I actually haven't.
- 59:05I've stuck on 1.5 for a long time.
- 59:11Now it's out.
- 59:12One of the things I'll add is that the velocity of the project is, it seems to be really high.
- 59:16And, you know, it's encouraging to see all the contributions and all the edits that everybody's making.
- 59:22And so I, for one, am really excited about what it's going to look like in a year.
- 59:26You know, I think we've got a lot of opportunity ahead of us and, you know, looking forward to seeing, you know, everybody jump in and try things out.
- 59:33And, you know, if something doesn't work the way that you want it to, feel free to open up an issue or hop on Discord.
- 59:38We're all very responsive and would love to kind of hear what our users are thinking about as they build their applications.
- 59:45Seems like a great framework.
- 59:46I really like the balance you're striking between the micro frameworks and some of the batteries included.
- 59:52So congrats to all of you.
- 59:53Now, before we get out of here, just I'll ask one quick question, you know, for I usually ask at the end.
- 59:59And that is a quick shout out to some package or library out there that you like and want to spread the word about.
- 1:00:06I guess I'll start DuckDB for me.
- 1:00:08So I have used a massive amount of DuckDB and you can kind of think about it as like an analytical SQLite.
- 1:00:15And so it's in process.
- 1:00:16And so you can start it up and just run SQL directly from your Python process.
- 1:00:22And so the project that I'm working on at Google actually has quite a bit of DuckDB as kind of this like middle ETL piece where data gets ingested.
- 1:00:30We do things in DuckDB and then that actually gets exported to BigQuery or other database engines.
- 1:00:36And so this really has kind of opened up the flexibility of us to be able to do quite a bit of transformations just in RAM without having to write to disk.
- 1:00:43That's cool.
- 1:00:44And in process SQL lab database management system.
- 1:00:47Cool.
- 1:00:48He has been recruiting us to use DuckDB for quite a while now.
- 1:00:52He succeeded.
- 1:00:53That's cool.
- 1:00:55I love what the standard library has, but Click is like one of my favorite CLI building tools.
- 1:01:01Rich is the thing that makes you make your terminal beautiful.
- 1:01:06But there's this really cool package that just, I mean, you can use them both separately, but rich-click.
- 1:01:11And it's what we use for our CLI.
- 1:01:15You have the great Click CLI building stuff, but then rich on top of it automatically makes everything pretty.
- 1:01:22That's cool.
- 1:01:22So it's like all the magic and niceness of rich, but available for a click, like colors in your help documents.
- 1:01:30I haven't gotten to check out Sebastian's typer yet, but I've seen some screenshots and it's sort of similar.
- 1:01:34I don't know a whole lot about that, but...
- 1:01:37I think it also uses rich, right?
- 1:01:38I think so.
- 1:01:39Nice.
- 1:01:39All right.
- 1:01:40Janek.
- 1:01:40Well, for me, it's got to be msgspec.
- 1:01:42Because it's just...
- 1:01:44So if you do any kind of JSON parsing or serialization or msgspec parsing or serialization or data modeling that you might usually want to do with a data class and want to add a bit of validation on top, or, you know, just if you're curious, you should absolutely check this library out.
- 1:02:04Because it's super amazing.
- 1:02:06The author is really, really great.
- 1:02:09I can just give him just a huge shout out because he's done such a great job at supporting our integration with it.
- 1:02:16It has been quite a tight collaboration at some points because when we started integrating it, there were a lot of things where we felt like, okay, so, well, we kind of can't use it right now because of this reason or that reason.
- 1:02:30And he's been so responsive and helpful in finding ways for us to work around that or just straight up implementing features that were missing for us.
- 1:02:39And it's really...
- 1:02:41That's pretty awesome.
- 1:02:41I can't thank him enough.
- 1:02:43It's really great.
- 1:02:44It's a pleasure to work with him.
- 1:02:46And it's an awesome library that everyone should check out, I think.
- 1:02:50Cool.
- 1:02:51That's news to me, so I will definitely check it out.
- 1:02:53All right.
- 1:02:53Well, thank you all for being here.
- 1:02:55Final call to action.
- 1:02:56People want to get started with Litestar.
- 1:02:58What do you tell them?
- 1:02:59Go to lightstar.dev.
- 1:03:00You can read the docs.
- 1:03:01Use it 15 minutes, 30 minutes.
- 1:03:03You'll know if you like it or not.
- 1:03:04Join us on Discord if you have questions.
- 1:03:06We're happy to help answer anything that may come up.
- 1:03:09I don't think I can add anything valuable to that anymore.
- 1:03:12All right.
- 1:03:14Well, yeah.
- 1:03:15Sounds good, guys.
- 1:03:16Thank you for being here.
- 1:03:16Congrats on the project.
- 1:03:18Thank you, Michael.
- 1:03:18Bye-bye.
- 1:03:19This has been another episode of Talk Python To Me.
- 1:03:23Thank you to our sponsors.
- 1:03:24Be sure to check out what they're offering.
- 1:03:26It really helps support the show.
- 1:03:27Take some stress out of your life.
- 1:03:30Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:03:35Sentry.
- 1:03:35Just visit talkpython.fm/sentry and get started for free.
- 1:03:40And be sure to use the promo code TALKPYTHON, all one word.
- 1:03:44Want to level up your Python?
- 1:03:46We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:03:50Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:03:55And best of all, there's not a subscription in sight.
- 1:03:58Check it out for yourself at training.talkpython.fm.
- 1:04:01Be sure to subscribe to the show.
- 1:04:03Open your favorite podcast app and search for Python.
- 1:04:06We should be right at the top.
- 1:04:07You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:04:12and the direct RSS feed at /rss on talkpython.fm.
- 1:04:17We're live streaming most of our recordings these days.
- 1:04:19If you want to be part of the show and have your comments featured on the air,
- 1:04:23be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:04:27This is your host, Michael Kennedy.
- 1:04:29Thanks so much for listening.
- 1:04:30I really appreciate it.
- 1:04:32Now get out there and write some Python code.
- 1:04:33I'll see you next time.