Latest / Talk Python To Me / #556: Updates on Django's Async Story
Transcript
- 0:00Michael Kennedy: For years, Django and Async came with an asterisk.
- 0:04Michael Kennedy: The docs themselves warned you off it.
- 0:06Michael Kennedy: Scary performance notes.
- 0:08Michael Kennedy: A story that felt half-finished.
- 0:10Michael Kennedy: Well, that story just got rewritten, literally.
- 0:12Michael Kennedy: And the person who rewrote it is here to tell you why that old framing was wrong.
- 0:17Michael Kennedy: Carlton Gibson is a former Django fellow, sat on the security team for eight years,
- 0:22Michael Kennedy: and he's on the steering council.
- 0:24Michael Kennedy: On this episode, we get into the Async topic doc rewrite.
- 0:27Michael Kennedy: what actually remains versus what was just fear and the new tasks framework in 6.0,
- 0:33Michael Kennedy: DB-level cascades and fetch models landing in 6.1,
- 0:36Michael Kennedy: and why free threading is the bet it's about to pay off big for Django.
- 0:41Michael Kennedy: If you've been told that Django's async story isn't ready,
- 0:44Michael Kennedy: this episode puts that myth to bed.
- 0:46Michael Kennedy: This is Talk Python To Me, episode 556, recorded July 2nd, 2026.
- 1:09Michael Kennedy: Welcome to Talk Python To Me, the number one Python podcast for developers and data scientists.
- 1:14Michael Kennedy: This is your host, Michael Kennedy. I'm a PSF fellow who's been coding for over 25 years.
- 1:20Michael Kennedy: Let's connect on social media.
- 1:22Michael Kennedy: You'll find me and Talk Python on Mastodon, Bluesky, and X.
- 1:25Michael Kennedy: The social links are all in your show notes.
- 1:27Michael Kennedy: You can find over 10 years of past episodes at talkpython.fm.
- 1:31Michael Kennedy: And if you want to be part of the show, you can join our recording live streams.
- 1:35Michael Kennedy: That's right.
- 1:35Michael Kennedy: We live stream the raw uncut version of each episode on YouTube.
- 1:39Michael Kennedy: Just visit talkpython.fm/youtube to see the schedule of upcoming events.
- 1:44Michael Kennedy: Be sure to subscribe there and press the bell so you'll get notified anytime we're recording.
- 1:48Michael Kennedy: This episode is brought to you by Sentry.
- 1:50Michael Kennedy: Don't let those errors go unnoticed. Use Sentry like we do here at Talk Python. Sign up at
- 1:55Michael Kennedy: Talk Python.fm slash Sentry. Carlton, welcome back to Talk Python To Me. Amazing to have you here.
- 2:01Michael Kennedy: Thank you, Michael. Thank you for having me on again.
- 2:03Michael Kennedy: Always fun to talk with you. Fun to have you back. We're going to talk about one of my favorite
- 2:08Michael Kennedy: topics, async, both for the reasons it's amazing and the reasons it's like, why? Why is it this way?
- 2:15Carlton Gibson: Why do we do this to ourselves is the thing, right?
- 2:18Michael Kennedy: Exactly.
- 2:20Michael Kennedy: Wait, I want to say, oh, but I didn't want it like that.
- 2:22Michael Kennedy: No, just kidding.
- 2:24Michael Kennedy: Specifically with regard to Django, you did some really interesting updates with Django
- 2:29Michael Kennedy: that we're going to talk about.
- 2:30Michael Kennedy: But first, your teams, they're still in the World Cup.
- 2:33Michael Kennedy: How are we doing?
- 2:34Carlton Gibson: Oh, yeah.
- 2:34Carlton Gibson: Well, so I'm British by birth, so English by birth.
- 2:37Carlton Gibson: So I was supposed to support England, but I live in Spain.
- 2:40Carlton Gibson: So depending on who's winning, who's doing better, I get to flip my allegiance.
- 2:44Michael Kennedy: you know i think that's really that's really amazing because you if one of your team's not
- 2:50Michael Kennedy: doing well you can just like i'm gonna just cheer for the other one it's it's great you get like two
- 2:54Carlton Gibson: shots at the thing yeah i mean in the last uh european championships we had the england spain
- 2:59Carlton Gibson: final so i couldn't lose really yeah that's amazing yeah so the u.s team um advanced yesterday as well
- 3:06Michael Kennedy: they played really well so i'm very very excited for that yeah it's lovely to see new new nation
- 3:11Michael Kennedy: emerging nations do well in the World Cup.
- 3:12Michael Kennedy: You know, a long time ago, that would have been like some kind of joke.
- 3:15Michael Kennedy: People are like, ah, surely you're joking.
- 3:17Michael Kennedy: Surely you're joking about this.
- 3:19Michael Kennedy: But honestly, soccer or football, non-American football, is popular in America.
- 3:25Michael Kennedy: So super cool.
- 3:26Michael Kennedy: Glad to hear it.
- 3:27Michael Kennedy: And you're back.
- 3:29Michael Kennedy: So tell people just what have you been up to?
- 3:32Michael Kennedy: You know, not everyone listens every episode.
- 3:34Michael Kennedy: So a quick introduction for yourself, maybe.
- 3:36Carlton Gibson: Okay.
- 3:36Carlton Gibson: So my name is Carlton.
- 3:38Carlton Gibson: I've long time worked with Django.
- 3:40Carlton Gibson: I'm a former Django fellow, did that for five years.
- 3:42Carlton Gibson: I'm on the Django steering council.
- 3:44Carlton Gibson: I was on the security team for eight years.
- 3:46Carlton Gibson: I maintain a whole load of packages in the ecosystem.
- 3:48Carlton Gibson: So, you know, I'm quite vested there.
- 3:51Carlton Gibson: And, yes, you know, basically I use Django and have done for, I don't know, however many years to build web applications to pay the bills.
- 4:01Carlton Gibson: Had a busy spring.
- 4:02Carlton Gibson: I was everywhere I went to.
- 4:04Carlton Gibson: There was a Pi TV event organized by JetBrains in Amsterdam in March that I went to.
- 4:09Carlton Gibson: That was all on YouTube.
- 4:10Carlton Gibson: There was, I don't know, 11 hours of live streaming or something.
- 4:13Carlton Gibson: It was amazing.
- 4:14Carlton Gibson: Then I was in DjangoCon Europe in Athens in, I think, May.
- 4:19Carlton Gibson: No, April.
- 4:20Carlton Gibson: And then in May, I was at Paikon Italia, which is in Bologna.
- 4:23Carlton Gibson: Paikon Italia really is a lovely conference.
- 4:26Carlton Gibson: You should make your way there.
- 4:27Carlton Gibson: And now I'm just home and I'm exhausted.
- 4:30Carlton Gibson: I had a quick holiday with the family, but now I'm hiding at home until September,
- 4:34Carlton Gibson: where we've got Django on the Med, which is a three-day sprint event,
- 4:37Carlton Gibson: which is going to be in Pescara, Italy, which we're looking forward to.
- 4:40Michael Kennedy: That sounds really, really nice.
- 4:43Michael Kennedy: I love traveling, but the jet lag kind of blows me out eventually.
- 4:47Michael Kennedy: I'm like, ah, this is 11 hours jet lag.
- 4:50Michael Kennedy: I don't really want to do this anymore.
- 4:52Michael Kennedy: It sounds to me like you're kind of able to keep it in the broader Euro neighborhood, which is nice.
- 4:57Carlton Gibson: Yes, I was.
- 4:58Carlton Gibson: I was, which it does help because there isn't that massive time change.
- 5:01Carlton Gibson: But it's still hard work, right?
- 5:03Carlton Gibson: Even though you have fun at these events, even though you're seeing your friends,
- 5:06Carlton Gibson: even though it's, you know, doing what you're vested in.
- 5:09Carlton Gibson: It's their long days and you have to be full on, you know, in action all day, each day.
- 5:15Carlton Gibson: And so by the time you get home, you are, you know, kind of exhausted.
- 5:17Carlton Gibson: And then obviously you've been away and you get home and, you know, you need to be present for your family too.
- 5:22Carlton Gibson: So there's a limit to how many of these I can do.
- 5:24Michael Kennedy: So I won't do any other events this year because it's just too much.
- 5:28Michael Kennedy: Yeah, that's good.
- 5:28Michael Kennedy: Just too much.
- 5:29Carlton Gibson: Yeah.
- 5:29Michael Kennedy: I remember traveling when we had little kids and that was a whole nother level because you'd come back.
- 5:33Michael Kennedy: It's like, you've been gone for a week, so I need some help.
- 5:37Michael Kennedy: But I'm exhausted.
- 5:38Michael Kennedy: You don't understand.
- 5:39Michael Kennedy: But at the same time, it's a fair request, you know?
- 5:41Carlton Gibson: Yeah, no, absolutely.
- 5:43Carlton Gibson: Absolutely.
- 5:43Carlton Gibson: I mean, our kids are older now.
- 5:45Carlton Gibson: The eldest is 18, or will be 18 next week, just about to go to university, and then 16,
- 5:50Carlton Gibson: and 13, 14.
- 5:51Carlton Gibson: So they are a lot easier than they used to be.
- 5:53Carlton Gibson: But still, it's a big ask on my partner to go away and they're alone for the whole week.
- 5:58Carlton Gibson: Absolutely.
- 5:59Carlton Gibson: In that point position, we might call it.
- 6:03Michael Kennedy: Yes, exactly.
- 6:04Michael Kennedy: so uh tell people you mentioned django on the med yes i talked to paulo about this a couple episodes
- 6:09Michael Kennedy: ago but yeah tell me okay you're going to be part of this too yeah yeah yeah so paulo and i
- 6:14Carlton Gibson: this was paulo's idea several years ago to have core developer sprints for django um because at the
- 6:19Carlton Gibson: the end of jank on europe or the end of jank on yes we always have a couple of days of sprints
- 6:23Carlton Gibson: and they're some of the best parts of the conference um but also it's around the time
- 6:28Carlton Gibson: people are leaving and your people are already kind of exhausted you know having the three days
- 6:32Carlton Gibson: of talks plus then of the sprints so it's like we wanted more and um we decided to to do this event
- 6:39Carlton Gibson: and we were like yeah this is a good idea but it didn't happen and then i think dublin a year a
- 6:44Carlton Gibson: year and a half ago now uh we were like no come on let's like it so we said we're going to have
- 6:47Carlton Gibson: it in palo vijay which is my hometown um we did that last september and all last october sorry
- 6:52Carlton Gibson: beginning of october and we're doing it in paulo's hometown pescara this september and it's as i say
- 6:57Carlton Gibson: It's three days to get together and work on Django.
- 6:59Carlton Gibson: And we had some really big features landed.
- 7:02Carlton Gibson: Like, for instance, Django 6.1 is in pre-release now.
- 7:06Carlton Gibson: And it's adding an ability to have DB-level cascade options for the on-delete.
- 7:10Carlton Gibson: So on-delete is, well, you've got a foreign key, so I've got a user, and that user has a bookmark.
- 7:16Carlton Gibson: Well, what happens when I delete the user?
- 7:18Carlton Gibson: What happens to the bookmark?
- 7:19Carlton Gibson: It's got a parent relationship.
- 7:21Carlton Gibson: And so normally you would cascade that.
- 7:23Carlton Gibson: Now, Django's always offered this option, but historically, it's done that in memory.
- 7:28Carlton Gibson: In Python, it's pulled, it's walked the tree, and it's got the objects, and then it's deleted the leaves, and then the stems, and only finally down to the branches.
- 7:37Carlton Gibson: And now, that can be very costly at scale.
- 7:41Michael Kennedy: I'm going to delete this one record that references several tables with a million entries.
- 7:46Michael Kennedy: That could be a lot of money.
- 7:47Carlton Gibson: Exactly.
- 7:48Carlton Gibson: You've got an organization that's got team members and projects, and you delete the organization.
- 7:53Carlton Gibson: it just brings down your application basically you get out of memory memory issues so the database
- 7:59Carlton Gibson: still has to do that work but the database is much much much better at doing that work than python is
- 8:05Carlton Gibson: um and so that's so anyway we've got this new option this ticket had been open for i don't know
- 8:10Carlton Gibson: so many years because it's it was really hard and we had marish feliciak who's a former django fellow
- 8:15Carlton Gibson: and one of the core um orm contributors simon charette is one of the core open source contributors
- 8:21Carlton Gibson: Lily Foote, Jacob Tyler-Wells, who's the new Django fellow. We had the four of them just there working
- 8:27Carlton Gibson: together and they were able to get this over the line. And it's literally something which would
- 8:32Carlton Gibson: never have happened except for the time that was available at Django on the Met. So we've got
- 8:38Carlton Gibson: exciting plans for this time. We're hoping to get people again, people from around the community to
- 8:42Carlton Gibson: come. And I know, I'm really excited about it. The first one was a proof of concept. It went really
- 8:47Carlton Gibson: well. The second one's lining up to be amazing too. And once we've done two, well, it's kind of
- 8:50Michael Kennedy: established and you know we're yeah well it's tradition you gotta do it yeah yeah exactly you
- 8:56Michael Kennedy: gotta go hang out with the mediterranean so i've done i've done a lot of remote work in my life
- 9:01Michael Kennedy: and i find you know when we do in-person meetups get the people who are all distributed together
- 9:06Michael Kennedy: it just changes the energy you know there's something about the bandwidth of face-to-face
- 9:12Carlton Gibson: communication which just isn't rec replicable record you which you can't replicate replica
- 9:18Carlton Gibson: online yeah thank you mike thank you my but yeah it's it's i don't know i mean look it works online
- 9:25Carlton Gibson: but as well we had another it was a good session as well so with um django we've got this um we've
- 9:30Carlton Gibson: had recently this new features board which was an attempt to take people suggesting features away
- 9:35Carlton Gibson: from django's track instance which is kind of old and scary and is a place for for the work to be
- 9:40Carlton Gibson: done and put it into a GitHub project and where we could have a project board and we could have
- 9:45Carlton Gibson: upvotes and we could have a Kanban board to see what were being advanced to try and give a bit
- 9:50Carlton Gibson: more visibility into the process. And sometimes there's disagreements and those disagreements
- 9:54Carlton Gibson: online, they can go on forever and ever, but we were able to have a session and there were
- 9:58Carlton Gibson: different voices from different opinions. And we were able to come to agreements in five minutes
- 10:03Carlton Gibson: on topics which had been rolling on the boards for months. And that kind of, look, I can see
- 10:10Carlton Gibson: why you're saying this. It's really hard to get that online. You know, there's money emojis
- 10:15Michael Kennedy: we use as much as we try and communicate openly it's just as much slack as you want to throw out
- 10:20Michael Kennedy: there and you know i mean since 2020 it's it's kind of been just oh yeah obviously remote is better
- 10:27Michael Kennedy: and so on and there are mega advantages don't get me wrong but it's not always better and in lots of
- 10:31Michael Kennedy: ways so yeah so i guess for this good gone you you mentioned 2020 and it's covid it's one thing that
- 10:37Carlton Gibson: um i was um django fellow over over that period and um you know it was a really hard time for
- 10:43Carlton Gibson: because i think that the pandemic really knocked the back out of the community not we had we
- 10:48Carlton Gibson: had older people stepping away we had young roots that were starting to come and then the put the
- 10:52Carlton Gibson: the pandemic came and it was kind of like this really really really tough period and it's now
- 10:58Carlton Gibson: five six years later that we're looking at it we're thinking actually now the community you know the
- 11:02Carlton Gibson: activity is going on with things like jang and on space and the new community the new members that
- 11:06Carlton Gibson: are coming through and the people who who are active in the community think well next year when the
- 11:11Carlton Gibson: steering council is re-elected. There's obvious candidates who weren't there two years ago.
- 11:18Carlton Gibson: And it's taken us, I think, half a decade to recover from the real knock that was COVID.
- 11:24Michael Kennedy: Wow. I believe it. I believe it. So is this for just for core devs or is this open to people who
- 11:30Michael Kennedy: just want to contribute? No, it's open to everybody. Obviously, if you're an established
- 11:36Carlton Gibson: contributor, it's going to be easy to get going. But it's also about bringing on new
- 11:41Carlton Gibson: and teaming people up.
- 11:43Carlton Gibson: And, you know, if you come
- 11:46Carlton Gibson: and you've got a particular interest in Django,
- 11:48Carlton Gibson: if you're a total Django novice,
- 11:50Carlton Gibson: it would be strange to come to choose Django on the Met
- 11:52Carlton Gibson: as your first event.
- 11:54Carlton Gibson: But if you're into Django and you know it
- 11:56Carlton Gibson: and you want to get contributing,
- 11:57Carlton Gibson: it's, yeah, that's part of the plan too,
- 12:00Carlton Gibson: is that we have both experienced and new contributors
- 12:03Carlton Gibson: and we can team people up and get people working together.
- 12:05Carlton Gibson: And, you know, part of the contributing to Django problem
- 12:09Carlton Gibson: is the learning curve.
- 12:10Carlton Gibson: So it feels like it's a big, scary thing.
- 12:12Carlton Gibson: Whereas if you sit down with somebody in a room and you work on what they're working on, you're like, actually, I could work on this side ticket.
- 12:17Carlton Gibson: And all of a sudden people are off.
- 12:19Michael Kennedy: Well, it suffers from what many of the successful projects out there do.
- 12:23Michael Kennedy: It's super polished.
- 12:25Michael Kennedy: It's used by millions and millions of projects.
- 12:27Michael Kennedy: It's hard to just look at it and go, oh, I'll just change this without regard to all that knock on effect, right?
- 12:33Carlton Gibson: Yes. And that's where part of the new features repo was about giving a space where people could put their ideas forward without having them immediately closed as won't fix.
- 12:45Carlton Gibson: Because if you went and, you know, oh, can we integrate React? No, we're not going to do that.
- 12:52Carlton Gibson: You can use React and here's how we use React, but we're not going to integrate it into JankRusate as an example.
- 12:57Carlton Gibson: And that would be open, it'd be no, no, close, won't fix.
- 13:00Carlton Gibson: And people felt that that was a little bit too...
- 13:03Carlton Gibson: A little too stack over flowy.
- 13:04Carlton Gibson: Well, yeah, it wasn't ideal.
- 13:06Carlton Gibson: So the new features repo is to allow space to develop for a conversation.
- 13:11Carlton Gibson: And then those ideas which have a bit of traction, we can say, no, do you know what?
- 13:14Carlton Gibson: A lot of people have said they support this one or we can advance it.
- 13:18Michael Kennedy: Hey, one second.
- 13:19Michael Kennedy: Normally, this would be an ad break from Sentry, but not this time.
- 13:23Michael Kennedy: Let's just thank them for supporting the show and get right back to the conversation.
- 13:27Michael Kennedy: Also, visit talkpython.fm/sentry after the show.
- 13:31Michael Kennedy: Thanks, Sentry.
- 13:32Michael Kennedy: You have another project coming along, The Mantle.
- 13:35Michael Kennedy: Tell us what this is.
- 13:37Carlton Gibson: Django Mantle.
- 13:38Carlton Gibson: So it's my take on a modern serialization story for Django.
- 13:43Carlton Gibson: So the big topic we've had is about REST Framework, and REST Framework is aging.
- 13:46Carlton Gibson: And REST Framework is wonderful, and it has been wonderful.
- 13:49Carlton Gibson: But the serializers in particular, they're kind of last generation.
- 13:52Carlton Gibson: And there are much more modern approaches available now, involved in Pydantic, in msgspec, in Attrs and Catters, which is what I use,
- 14:01Carlton Gibson: are much, much faster than they used to be.
- 14:04Carlton Gibson: They generate code which is almost as quick
- 14:07Carlton Gibson: as you would see if you hand-wrote it.
- 14:09Carlton Gibson: If you wrote the code to literally pull this attribute,
- 14:12Carlton Gibson: this attribute, this attribute,
- 14:13Carlton Gibson: the code that's generated for you by, say, caters
- 14:16Carlton Gibson: is as quick as that code, which is amazing.
- 14:19Carlton Gibson: Whereas REST Framework serializes,
- 14:21Carlton Gibson: they go through and get the field object from the model.
- 14:24Carlton Gibson: They get the value from it.
- 14:26Carlton Gibson: It's like, hang on, all of those loopings
- 14:28Carlton Gibson: are very slow by comparison.
- 14:31Carlton Gibson: So then the question is, well, why don't we just use Pydantic?
- 14:34Carlton Gibson: Well, Pydantic's fantastic, but it's not ORM aware.
- 14:37Carlton Gibson: It doesn't have any insight into how Django works or anything.
- 14:42Carlton Gibson: So Django Mantle is my take on that.
- 14:44Carlton Gibson: It's the type-save wrapper around Django's liquid core.
- 14:49Carlton Gibson: So you write these atters classes, and then you just instantiate a query object,
- 14:54Carlton Gibson: and you go fetch, and it fully instantiates your object to the shape you define,
- 15:00Carlton Gibson: and it fetches all the data all in one go,
- 15:02Carlton Gibson: and it can do that for nested objects,
- 15:04Carlton Gibson: and you can do writes with validation, all these things.
- 15:07Carlton Gibson: And it's ORM aware in that it only selects the fields that it needs,
- 15:11Carlton Gibson: and it does the pre-fetch relateds for you
- 15:14Carlton Gibson: so that you don't get this N plus one query problem, right?
- 15:17Carlton Gibson: And it's very good for performant fetches
- 15:20Carlton Gibson: of just the exact data you want.
- 15:23Carlton Gibson: You end up with your objects being fully Python typed.
- 15:27Carlton Gibson: They're at its classes, so they're typed.
- 15:28Carlton Gibson: Your type checker knows the way around them.
- 15:30Carlton Gibson: mypy or PyRefly is now adding support for Atis classes too, I see this week.
- 15:36Carlton Gibson: Oh, cool.
- 15:36Carlton Gibson: So anyway, it's an attempt to show what a serialization story using these modern approaches
- 15:43Carlton Gibson: can look like.
- 15:44Carlton Gibson: And then from there, it's like, well, okay, what does an API framework in 2026 look?
- 15:48Michael Kennedy: I love it.
- 15:49Michael Kennedy: I love this idea.
- 15:49Michael Kennedy: I think there's a lot of value in taking some of these very focused, lightweight, dedicated
- 15:56Michael Kennedy: like data structures right like c attrs or pydantic or just straight data classes and somehow
- 16:03Michael Kennedy: integrating them in here without just this heavyweight stuff like you talked about the
- 16:07Michael Kennedy: eager loading that's and some of the other things like the typing can be weird as well like sql
- 16:12Michael Kennedy: alchemy you kind of have to lie to it yes somehow do you either lie about it being a database thing
- 16:18Michael Kennedy: or do you lie about it being an integer when it actually gets the value right like yeah yeah yeah
- 16:24Carlton Gibson: Yeah, because the actual type is a Django deferred field, right?
- 16:28Carlton Gibson: For a Django modifier, it's a deferred field.
- 16:30Carlton Gibson: What on earth is a deferred field?
- 16:32Carlton Gibson: Where it's an object that has the descriptive protocol
- 16:35Carlton Gibson: so that when you access it, it gives you the converted value.
- 16:38Carlton Gibson: I don't want to type that.
- 16:40Michael Kennedy: Exactly.
- 16:41Michael Kennedy: Even if you do, then you try to add it to an integer.
- 16:43Michael Kennedy: It's like, you can't add that to an integer.
- 16:44Michael Kennedy: That's not the same thing.
- 16:45Michael Kennedy: You're like, but it's going to be.
- 16:46Carlton Gibson: Yeah, it's going to be.
- 16:47Carlton Gibson: So this was the topic of my talk that I gave in Amsterdam
- 16:52Carlton Gibson: and Django in Europe and PyCon in Thailand.
- 16:53Carlton Gibson: It's slightly different versions because I had more time at DjangoCon Europe.
- 16:57Carlton Gibson: So I went more into the way we grow from, say, an active record pattern,
- 17:02Carlton Gibson: which is like a Django model, right?
- 17:03Carlton Gibson: It's an active record.
- 17:04Carlton Gibson: It's your database row, but it's also an object that's got logical behaviors
- 17:09Carlton Gibson: to a more separated approach where you're like, hang on,
- 17:12Carlton Gibson: I don't want my display logic and my aggregation logic
- 17:16Carlton Gibson: and my user validation logic all living on this one database class.
- 17:20Carlton Gibson: So how do we separate those off?
- 17:22Carlton Gibson: That's kind of where mantle came from originally.
- 17:24Carlton Gibson: But the idea was that, and my take is that Django's ORM is built on these very powerful dynamic patterns that Python allows.
- 17:33Carlton Gibson: And retrofitting static typing onto that is going to be a real problem.
- 17:36Carlton Gibson: So instead of that, that's also a dynamic C.
- 17:38Carlton Gibson: On top of that, we have these static islands where we can have all the nice type safety features of modern Python,
- 17:44Carlton Gibson: but we're not trying to eliminate the very powerful dynamic features that we have underneath.
- 17:50Carlton Gibson: And so the mantles, the shape classes that you define, they're your static islands.
- 17:55Carlton Gibson: And there you can add all your domain logic that you want to, and you can have it fully
- 17:58Carlton Gibson: separated from the database.
- 18:00Carlton Gibson: And they are just pure Python classes.
- 18:01Carlton Gibson: There's no data mutability in there.
- 18:03Carlton Gibson: There's no like, say, oh, I modified a property and saved, and now I've updated something.
- 18:09Carlton Gibson: No, there's none of that active record stuff going on.
- 18:11Carlton Gibson: So it's a kind of way of getting some of the benefits of this sort of separation layers
- 18:17Michael Kennedy: type approach whilst still keeping everything you love from the aura uh spoken like someone who
- 18:23Michael Kennedy: appreciates some nice architecture and layers are very cool well the issue is as your application
- 18:28Carlton Gibson: grows the complexity grows with it and you need more structure to manage that complexity like you
- 18:35Carlton Gibson: simply do like it the issue isn't that you started with a Django model it's that your Django model grew
- 18:40Carlton Gibson: and then grew and it grew and all of a sudden it's got it's got methods on it which are several
- 18:45Carlton Gibson: distinct responsibilities. And then you think, oh, I'm getting a lot of code here. So you start
- 18:50Carlton Gibson: doing dry, you start abstracting methods. But that abstracted method wasn't for a single
- 18:55Carlton Gibson: responsibility. So it's not clean so that you've got, ah. And everybody who's grown a Django app
- 19:01Carlton Gibson: has seen this. And it's perfectly natural and it's perfectly normal. It's just how applications grow.
- 19:05Carlton Gibson: And Mantle is kind of my take on, well, how do we deal with that? How do we grow from the active
- 19:10Carlton Gibson: record pattern that we began to, to something that's more robust. And it's not that you should
- 19:15Carlton Gibson: start with the more robust patterns because you're over-engineering them. There's a nice road we have
- 19:20Carlton Gibson: to walk between these two problems. And that's the joy of crafting the application over time,
- 19:25Michael Kennedy: right? That's why we're programmed. I love it. I love it. All right. Let's talk about Django 6.1.
- 19:35Michael Kennedy: Yes.
- 19:36Michael Kennedy: I think it's coming out.
- 19:38Michael Kennedy: It's not quite finished,
- 19:39Michael Kennedy: but there's a lot of features we're going to talk about
- 19:42Michael Kennedy: that are pretty exciting and related here, right?
- 19:43Michael Kennedy: A lot of new things coming out of the database, for example.
- 19:45Carlton Gibson: Yes.
- 19:46Carlton Gibson: So I think it's in beta now.
- 19:47Carlton Gibson: So it's going to be a release candidate in a couple of weeks,
- 19:49Carlton Gibson: and then we're about a month away from the final release.
- 19:52Carlton Gibson: Oh, by the way, I see you've got the page up there.
- 19:54Carlton Gibson: The Django Developer Survey is on right now.
- 19:56Carlton Gibson: Do take it, folks.
- 19:57Carlton Gibson: We want as many replies as we can.
- 19:58Carlton Gibson: It really does help drive forward the development of Django.
- 20:01Carlton Gibson: So do give that five minutes there of your time to answer the questions.
- 20:05Carlton Gibson: But what's exciting?
- 20:07Carlton Gibson: Well, we already talked about database-level cascades, right?
- 20:10Carlton Gibson: That's my big feature because that's something that you just couldn't work around.
- 20:13Carlton Gibson: And there's no in Python solution to that.
- 20:16Carlton Gibson: We wanted that.
- 20:17Carlton Gibson: You'd have to, well, in the past, I have created a custom database backend and changed the way it creates.
- 20:23Carlton Gibson: No, that's not a sustainable way.
- 20:25Carlton Gibson: So database-level cascades are my favorite one.
- 20:28Carlton Gibson: The one that I think more people are excited about is called fetch modes, which are on query sets.
- 20:33Carlton Gibson: So we talked about the prefetch related with the way Mantle handles it.
- 20:37Carlton Gibson: But the idea is you've got an author and so you fetch a user and they've got a list of bookmarks.
- 20:43Carlton Gibson: And so you get the author, you get the user, and then you iterate for, bookmark in users.bookmark, do display the bookmark.
- 20:51Carlton Gibson: And so what happens is it goes through and you get this N plus one query where it goes and fetches the lazy related object.
- 20:57Carlton Gibson: And that's great at small scales and when you're getting going, but it's a performance nightmare, basically.
- 21:05Carlton Gibson: And this N plus one problem that comes up is something that people hit all and all again.
- 21:10Carlton Gibson: So fetch modes let you say, look, fetch peers, which is the exciting one.
- 21:13Carlton Gibson: It lets you say, if I access the first element in a list of related objects, go and get them all at once.
- 21:20Carlton Gibson: And so it's like a deferred pre-fetch related.
- 21:22Carlton Gibson: And that's going to be a lot easier to manage in a lot of cases than having to manually add the pre-fetch related in each case, because those often become difficult to do by hand.
- 21:33Carlton Gibson: They're all right to add the first time, but they become difficult to keep updated.
- 21:36Carlton Gibson: And then you're suddenly pre-fetch relating something which you're no longer using.
- 21:40Carlton Gibson: And so fetch mode.
- 21:41Michael Kennedy: Yeah, your data access layer, maybe you're right, says get me all the books.
- 21:45Michael Kennedy: But then you might have to add like some kind of flag that says and the authors.
- 21:50Michael Kennedy: Because sometimes you want to call that function and you want the authors, but sometimes you
- 21:53Michael Kennedy: want to just list the books, you know, and you don't need that.
- 21:55Michael Kennedy: And it's like, yeah, so this way kind of just lets you says, if you're ever going to do
- 22:00Michael Kennedy: this lazy loading, then basically upgrade it to an eager load.
- 22:04Carlton Gibson: Yes.
- 22:04Carlton Gibson: And there's, I don't think the videos are quite out yet, but Jacob Tyler was, you found
- 22:09Carlton Gibson: a great talk at DjangoCon Europe on the, on how fetch modes work.
- 22:12Carlton Gibson: They'll, they'll be out very soon.
- 22:13Carlton Gibson: I think they'll be on YouTube, but do look out for them in the interest, but how, how,
- 22:17Carlton Gibson: how fetchmates work and what the limitations were and you know where it's not you know it's not a
- 22:22Carlton Gibson: magic solution it's not just oh let's enable fetch peers in all cases you know without thinking about
- 22:27Carlton Gibson: it no but it it's a good change and um when i've spoken to people about what they're excited about
- 22:32Carlton Gibson: in 6.1 most people have pointed to this as their headline feature nice nice so that's good and then
- 22:39Carlton Gibson: i guess um a little bit yeah go ahead well i was going to say about the other thing that i'm
- 22:43Carlton Gibson: excited about that you know there's an old list of features the thing with django releases every
- 22:47Carlton Gibson: there's this big long release pack go and read it go and pick your favorite features but there's
- 22:50Carlton Gibson: always lots in there for everything but you we can now customize the multi-part parser class for
- 22:57Carlton Gibson: on the request object now whoever wants to do that well it turns out but that you can when you're
- 23:03Carlton Gibson: getting multi-part requests if you're getting big ones with lots of multi-parts in swapping out a
- 23:08Carlton Gibson: different multi-parts class could save you maybe 30 on your passing time which is 30 cpu on pass it
- 23:14Carlton Gibson: on request for a passing time,
- 23:16Carlton Gibson: that may be a non-negagable game.
- 23:18Carlton Gibson: So I'm excited about that.
- 23:19Carlton Gibson: But I'm excited because it starts to open the pathway
- 23:23Carlton Gibson: to more customizations on the request.
- 23:25Carlton Gibson: And so, you know, Django's got this implementation,
- 23:28Carlton Gibson: but can we swap out that implementation and compare it
- 23:31Carlton Gibson: and see if we can get some performance gains
- 23:33Carlton Gibson: by being able to turn some of these nods under the hood.
- 23:36Michael Kennedy: That's really nice.
- 23:37Michael Kennedy: And having it be customizable is really great.
- 23:39Michael Kennedy: I'm working on this.
- 23:40Michael Kennedy: I'm working with this project that I just want to use.
- 23:43Michael Kennedy: I don't want to be an author of it, but it has no real extensibility.
- 23:48Michael Kennedy: So anything I want to do, I have to write little custom bits and change the source code and run it.
- 23:52Michael Kennedy: And I'm like, as soon as there's a new version, how am I going to pay for this?
- 23:56Michael Kennedy: You know what I mean?
- 23:56Michael Kennedy: And so this is really nice.
- 23:58Carlton Gibson: Yeah.
- 24:00Carlton Gibson: Again, this is customizing the multi-part pass class.
- 24:04Carlton Gibson: I don't think that's for the majority of people.
- 24:06Carlton Gibson: But when you need it, it's going to be there.
- 24:08Carlton Gibson: And it's like, okay, actually, there's a big win.
- 24:10Carlton Gibson: So those are my favorites.
- 24:12Michael Kennedy: Yeah.
- 24:12Michael Kennedy: Okay.
- 24:13Michael Kennedy: What's the timeline?
- 24:14Michael Kennedy: It's in beta, you say?
- 24:15Carlton Gibson: It's in beta now.
- 24:16Carlton Gibson: So the pre-release is three months.
- 24:19Carlton Gibson: So the alpha was a month ago.
- 24:21Carlton Gibson: The beta is just now.
- 24:23Carlton Gibson: And then the release candidate will be in a week or so.
- 24:25Carlton Gibson: And then a couple of weeks after that will be the final.
- 24:27Carlton Gibson: So I guess August will be.
- 24:29Carlton Gibson: So it's coming fast.
- 24:31Carlton Gibson: Oh, yes, yes, yes.
- 24:32Carlton Gibson: It's very nearly there.
- 24:34Carlton Gibson: Excellent.
- 24:35Carlton Gibson: So just a call to action.
- 24:37Carlton Gibson: If you're using Django and you could just install it against your application and run the beta,
- 24:41Carlton Gibson: run your test suite against the beta.
- 24:43Carlton Gibson: That would be amazing because if it passes, then you're all good.
- 24:46Carlton Gibson: You're good to update.
- 24:46Carlton Gibson: If it doesn't pass, then we want to know that.
- 24:48Carlton Gibson: We want to know, well, what changed?
- 24:51Michael Kennedy: It's got to be such a challenge to work on a project that's used,
- 24:55Michael Kennedy: not just consumed by people, but as a programmer,
- 24:59Michael Kennedy: code integrated with this project, right?
- 25:01Carlton Gibson: Yeah, yeah, yeah.
- 25:02Carlton Gibson: And there's the whole ecosystem, right?
- 25:03Carlton Gibson: So, I mean, why do people use Django?
- 25:06Carlton Gibson: One of the big reasons they use Django is for the third-party packages
- 25:09Carlton Gibson: that are available, CMSs or image processing packages or whatever it is that you want to do.
- 25:14Carlton Gibson: Did we break any integrations with them?
- 25:16Carlton Gibson: Ah, well, we hope not.
- 25:17Carlton Gibson: But unless people test and tell us, we don't find out.
- 25:19Carlton Gibson: And when I was a fellow for five years and in every release, we would have on release
- 25:24Carlton Gibson: day bug reports saying, oh, there's some regression here when we could have had those a week before
- 25:30Carlton Gibson: and fix them before the final release.
- 25:31Michael Kennedy: Yeah.
- 25:32Michael Kennedy: Have you looked lately what the...
- 25:36Michael Kennedy: I'll just do it like this.
- 25:37Michael Kennedy: Have you looked at what it says used by?
- 25:39Michael Kennedy: On GitHub here?
- 25:41Michael Kennedy: You know, it says, does it say how many projects?
- 25:43Michael Kennedy: I don't know.
- 25:44Michael Kennedy: Oh yeah, here we go.
- 25:45Michael Kennedy: This is just the public projects on GitHub,
- 25:47Michael Kennedy: but two million?
- 25:48Michael Kennedy: Couple of million.
- 25:49Michael Kennedy: Yeah, right.
- 25:49Michael Kennedy: Okay, good.
- 25:51Michael Kennedy: There's so many that are private.
- 25:53Michael Kennedy: There's so many that have nothing to do with GitHub.
- 25:54Michael Kennedy: It's like a very small sample.
- 25:56Carlton Gibson: That's still crazy.
- 25:58Carlton Gibson: Jay, a couple of years ago at DjangoCon US,
- 26:01Carlton Gibson: I think maybe 2024,
- 26:02Carlton Gibson: Jacob Kaplan Moss gave a sort of best estimate
- 26:04Carlton Gibson: about the user base, about 4 million,
- 26:06Carlton Gibson: something like that as a kind of you know four million in action django projects in in the wild
- 26:12Michael Kennedy: so okay you know if you count all the internal apps given that two million public repos on gmail
- 26:19Michael Kennedy: if you count all the internal private things that people just threw up for like a little internal
- 26:23Michael Kennedy: company app i bet it's way more than four million now i obviously couldn't prove it but yeah i mean
- 26:28Carlton Gibson: i'd have to ask jacob how he came up with the figures but it was in terms of downloads and
- 26:31Michael Kennedy: looking at this and looking yeah yeah exactly yeah probably the big query data pipe yeah or
- 26:36Michael Kennedy: Yeah. Honestly, I feel like that stuff's kind of broken these days. Like 10 years ago,
- 26:40Michael Kennedy: that was pretty representative. But right now, I think it's kind of a little weird on both ends.
- 26:46Michael Kennedy: I think it's weird because there's so many Docker builds, right? This project can say,
- 26:51Michael Kennedy: well, it got a hundred downloads. Well, that's because we just built something in CI a bunch
- 26:56Michael Kennedy: of times today. We didn't really intend to download it, that many projects. But also at the same time,
- 27:01Michael Kennedy: there's a bunch of caching layers. Like you might grab a Docker image that has it or build a Docker
- 27:05Michael Kennedy: image and never reload that or use uv caching like it's just so opaque i think i mean a lot of people
- 27:11Carlton Gibson: run little local pi pi pi cache or pip caches sorry so when you do pip install you're not you
- 27:17Carlton Gibson: fetched it once you but you might have installed it a hundred times from that so yeah exactly what's
- 27:22Michael Kennedy: that meant to be counted as one or i i think it's why it's so hard i don't have no idea so anyway
- 27:28Michael Kennedy: um incredible and this obviously is right i was gonna say once upon a time there was a an idea
- 27:33Carlton Gibson: to add some kind of telemetry into Django.
- 27:35Carlton Gibson: And obviously that didn't go anywhere
- 27:36Carlton Gibson: because no one wants to go anywhere near that.
- 27:38Carlton Gibson: But we still sometimes would like some idea
- 27:42Carlton Gibson: about how we estimate
- 27:44Carlton Gibson: because the sort of visible Django community
- 27:46Carlton Gibson: is like a thousandth of a percent.
- 27:48Carlton Gibson: So say Jacob's 4 million users isn't too far off.
- 27:51Carlton Gibson: The sort of visible community,
- 27:53Carlton Gibson: the people who subscribe to the newsletter
- 27:54Carlton Gibson: or do the survey or come to Django cons
- 27:57Carlton Gibson: or all these things,
- 27:58Carlton Gibson: they're about a thousandth of that,
- 28:00Carlton Gibson: like 4,000 or 5,000 people
- 28:02Carlton Gibson: that we can sort of point to and go,
- 28:03Carlton Gibson: I know who you are.
- 28:04Carlton Gibson: I know who you are.
- 28:05Carlton Gibson: Yes, I've heard of you.
- 28:06Carlton Gibson: Yes, I know you're on LinkedIn.
- 28:08Carlton Gibson: But that's a fraction of the total user base.
- 28:10Carlton Gibson: It would be lovely if we had some way
- 28:12Michael Kennedy: of just reaching more.
- 28:14Michael Kennedy: I know.
- 28:15Michael Kennedy: I have a new term for you, Carlton.
- 28:16Michael Kennedy: Dark matter Django.
- 28:18Carlton Gibson: Yeah, brilliant.
- 28:18Carlton Gibson: I'm having that.
- 28:19Carlton Gibson: Let me write that down.
- 28:21Michael Kennedy: Dark matter Django.
- 28:22Michael Kennedy: Well, we're talking about stuff
- 28:24Michael Kennedy: that is not dark matter.
- 28:27Michael Kennedy: So you recently did a bunch of work
- 28:31Michael Kennedy: on the documentation and positioning of async in general for django yeah yes yeah so the topic
- 28:39Carlton Gibson: guide which is here that you've got up on the page the asynchronous support topic guide it was first
- 28:43Carlton Gibson: written when we very first added um async support to django back in like that Django 3. something or
- 28:51Carlton Gibson: i can't remember exactly but i think it was um python 3.6 it was was when it is and um async's
- 28:58Carlton Gibson: come an awful long way since then and it was very negative it was like oh we still haven't finished
- 29:04Carlton Gibson: this it's still in development it's still not ready and the performance implications are terrible
- 29:10Michael Kennedy: unless you do it 100 perfectly but you and there was regular back in um go back in um python three
- 29:17Michael Kennedy: five three six days it async and await and this async concept really had kind of a two to three
- 29:24Michael Kennedy: issue like i would even if i wanted to do it i got to call this library which is talking to the
- 29:30Michael Kennedy: internet or into the database but it's not async so what's the point yes yes and the costs of so
- 29:36Carlton Gibson: the the big thing we'll come and talk about a lot is threats so in um django uh we use a lot of what
- 29:42Carlton Gibson: of a helper um from the asgirf library called sync to async where we um you take async function and
- 29:48Carlton Gibson: you you wrap it into a call routine so that you can wait into a coroutine so you can await that
- 29:53Carlton Gibson: and it gets run in a thread and async io has the two thread thing which is the same um but essentially
- 29:58Carlton Gibson: the same option um sync to async has some ability to track which thread it was called from so for
- 30:05Carlton Gibson: instance database connections they're tied to a thread um and the transaction state is all tied
- 30:11Carlton Gibson: to that thread so it's important that database um queries are run on the same thread as the connection
- 30:17Carlton Gibson: came from. Otherwise, you're going to have problems. Sync2AdsAsync adds a few niceties
- 30:24Carlton Gibson: around that, but it's essentially the same as AsyncIO2 thread. Go and run this in an executor
- 30:29Carlton Gibson: on a separate thread. That's great, but that used to cost orders of milliseconds to bootstrap and
- 30:36Carlton Gibson: set up. With latest pythons, it's tens of nanoseconds, or tens of microseconds, sorry.
- 30:42Carlton Gibson: Tens of microseconds when you're doing milliseconds requests, they're not a significant thing,
- 30:46Carlton Gibson: But the documentation was like, no, this is going to slow down your requests.
- 30:49Carlton Gibson: And it's like, hang on, it's really not.
- 30:53Carlton Gibson: And the API is complete.
- 30:55Carlton Gibson: The ORM, the cache framework, authentication, session, signals, it's all got async API.
- 31:01Michael Kennedy: So from a user perspective, it really is all there.
- 31:03Carlton Gibson: It doesn't mean there's no work to be done.
- 31:06Carlton Gibson: It's an ongoing project.
- 31:07Carlton Gibson: We keep developing it.
- 31:09Carlton Gibson: We keep fixing new things.
- 31:10Carlton Gibson: We keep adding new things.
- 31:11Carlton Gibson: But this idea that the topic guy presented that somehow the story wasn't finished, well, that's not quite, that wasn't quite right.
- 31:19Carlton Gibson: So we wanted to update that.
- 31:20Carlton Gibson: And we wanted to take some of the warnings about performance and just downgrade them to notes.
- 31:24Carlton Gibson: You know, they're a bit overplayed in this latter day.
- 31:27Michael Kennedy: I agree.
- 31:28Michael Kennedy: I think the sync to async and back and forth is pretty interesting.
- 31:32Michael Kennedy: And more broadly, you know, one of the things people use as a, I don't really want to use async.
- 31:38Michael Kennedy: It's kind of bad.
- 31:39Michael Kennedy: they'll call it like a virus or something and a lot of times what they mean is like well you've
- 31:43Michael Kennedy: got some low level function it's async so in order to call that the thing above it has to be async
- 31:47Michael Kennedy: which means like the rest of your app like it just cascades you know it's sort of viral in the gpl
- 31:51Michael Kennedy: sense right um but yes that's a you don't have to live with that you can yeah go ahead sorry well
- 31:56Carlton Gibson: they call it function coloring problem right is that once you've got a blue function or red
- 32:01Carlton Gibson: function all the functions are called it have to be the same color yes but but you there's nothing
- 32:07Michael Kennedy: to stop you from going in a synchronous function just async to sync boom and like kind of just stop
- 32:13Michael Kennedy: the propagation there or create your own async io event loop and just run that one bit down below and
- 32:20Michael Kennedy: so you could have a synchronous function but it maybe calls five external endpoints and you could
- 32:24Michael Kennedy: do all that with async in parallel and just like run that but cap it right there i think there's a
- 32:29Michael Kennedy: lot more creativity that we can bring just just just say like oh it's virus either it's amazing or
- 32:34Michael Kennedy: we don't want it at all, you know?
- 32:35Michael Kennedy: Yes.
- 32:35Carlton Gibson: No, that example,
- 32:37Carlton Gibson: that example you gave
- 32:38Carlton Gibson: of calling five,
- 32:39Carlton Gibson: you know, five external things concurrently,
- 32:42Carlton Gibson: that's one of the easiest ways
- 32:45Carlton Gibson: into AsyncVagenda.
- 32:45Carlton Gibson: So you've got your WSGI app,
- 32:46Carlton Gibson: it's deployed,
- 32:47Carlton Gibson: it's got its,
- 32:49Carlton Gibson: you've run Gunicorn,
- 32:50Carlton Gibson: it's got, you know,
- 32:51Carlton Gibson: four workers running,
- 32:52Carlton Gibson: saying handling requests,
- 32:53Carlton Gibson: each one in a single thread,
- 32:54Carlton Gibson: nice and separated,
- 32:55Carlton Gibson: so they're not getting
- 32:56Carlton Gibson: in each other's ways,
- 32:58Carlton Gibson: sorry, in a separate process,
- 32:59Carlton Gibson: each kind of single threaded,
- 33:01Carlton Gibson: doing its thing.
- 33:02Carlton Gibson: And then you're like,
- 33:02Carlton Gibson: I want to make some,
- 33:04Carlton Gibson: API requests.
- 33:06Carlton Gibson: So you can define a single async view
- 33:09Carlton Gibson: and Django's handlers will run that
- 33:12Carlton Gibson: in an event loop for you
- 33:14Carlton Gibson: and you can run, say, your five HTTP requests
- 33:17Carlton Gibson: and you can aggregate them together
- 33:19Carlton Gibson: and you can return a single response.
- 33:21Carlton Gibson: And that can just be done in a fraction of the time
- 33:24Carlton Gibson: of calling one and then the other.
- 33:26Michael Kennedy: Yeah, yeah, yeah.
- 33:27Michael Kennedy: And it's just so clearly a win, right?
- 33:31Michael Kennedy: Do you have to switch to an ASGI, an ASGI server mechanism if you're going to use async views in Django?
- 33:37Carlton Gibson: No, you can run async views under WSGI.
- 33:41Carlton Gibson: But what you don't get, this is a good question.
- 33:46Carlton Gibson: So say I'm deployed, say I'm a Django developer, when should I switch to ASCII?
- 33:51Carlton Gibson: When should I switch to async?
- 33:53Carlton Gibson: And the real question is if you want long-lived requests.
- 33:56Carlton Gibson: If you want to do, say, something where you hold open a connection and you, say, do service center events where every five seconds you get an update.
- 34:06Carlton Gibson: Like a stock ticker is a good example, right?
- 34:08Carlton Gibson: You get what was the latest stock price.
- 34:11Carlton Gibson: Okay, it keeps coming down the wire at you.
- 34:13Carlton Gibson: Well, you don't want to do that on the WSGI model because each connection is a worker on WSGI.
- 34:19Carlton Gibson: And so you say you've got four workers.
- 34:21Carlton Gibson: Well, that's great.
- 34:22Carlton Gibson: But then, well, hang on.
- 34:23Carlton Gibson: I've got three of those held open by clients.
- 34:25Carlton Gibson: well, now I've only got one serving requests.
- 34:28Carlton Gibson: Well, okay, double your workers.
- 34:30Carlton Gibson: But then you're going to double your users.
- 34:32Carlton Gibson: And if you're using pre-forked workers
- 34:35Carlton Gibson: where each worker lives in a separate process,
- 34:37Carlton Gibson: that's a lot of times that your Django application
- 34:39Carlton Gibson: is loaded into RAM and say the cache is primed
- 34:43Carlton Gibson: or the database connections are set up.
- 34:47Carlton Gibson: There's a lot of overhead to running multiple processes.
- 34:50Carlton Gibson: Whereas if you can do it with a NASGI worker,
- 34:51Carlton Gibson: you can hold open that connection
- 34:54Carlton Gibson: and it can just be served on the event loop.
- 34:56Carlton Gibson: You can have many connections just for one worker.
- 34:58Michael Kennedy: It's basically tied to an event loop
- 35:00Michael Kennedy: and then that can share that thread or that worker
- 35:04Michael Kennedy: as it interlaces across that one event loop, right?
- 35:06Michael Kennedy: Yeah, exactly.
- 35:07Carlton Gibson: And that's where the exciting things come.
- 35:09Carlton Gibson: If you've got long-lived connections,
- 35:12Carlton Gibson: even if you're just doing polling,
- 35:13Carlton Gibson: you can do polling quite quickly, once a second,
- 35:16Carlton Gibson: and you're not really going to overload your whiskey workers.
- 35:20Carlton Gibson: But if you're holding a connection open,
- 35:22Carlton Gibson: Ah, at that point, Async and ASCII is really called for.
- 35:26Carlton Gibson: Yeah, that's cool.
- 35:27Carlton Gibson: So you can kind of have this progressive scale.
- 35:30Carlton Gibson: Yeah, yeah.
- 35:31Carlton Gibson: I mean, standardly, the people that, I mean, we have an awful lot of people doing very exciting things, you know, with Async.
- 35:39Carlton Gibson: They're either running small projects where a single ASCII worker is enough and it can handle enough connections and you don't actually need multiple processes.
- 35:49Carlton Gibson: Or they tend to be running at the moment a WSGI base for their app,
- 35:53Carlton Gibson: so a standard WSGI base, which is handling most of the work.
- 35:56Carlton Gibson: And then the async endpoints are just running in a single ASCII process
- 36:00Carlton Gibson: as a sort of sidecar.
- 36:01Carlton Gibson: And they do this because something we'll get into is you get what's called
- 36:06Carlton Gibson: you get kind of contention for CPU-bound work on the event.
- 36:11Carlton Gibson: And we can talk about that in a bit.
- 36:12Carlton Gibson: But I think that's the way people are deploying most successfully
- 36:17Michael Kennedy: at the slightly larger applications.
- 36:19Michael Kennedy: Yeah, that makes sense. And this contention, it is very interesting. It certainly is. I want it actually, I searched for the wrong thing. Hold on. That is, I wanted to actually show. I recently went through this whole process of like, how could I make my server use less memory?
- 36:36Michael Kennedy: brought and i had some of my web apps were running purely synchronous as whiskey and some of them i
- 36:44Michael Kennedy: had upgraded to like true async running on asgi on granian right and i'm just i was looking the
- 36:51Michael Kennedy: server i'm like it's using i don't know nine gigs of ram for all these problems like 27 containers
- 36:56Michael Kennedy: running but still it's a lot i'm like is there a way that i could just maybe reduce the workers
- 37:01Michael Kennedy: a little bit and make it chill.
- 37:03Michael Kennedy: And so I wrote this article about like going through
- 37:06Michael Kennedy: all these different things.
- 37:07Michael Kennedy: But one of the things that I found really helpful was
- 37:11Michael Kennedy: if I could switch to more of the processes,
- 37:15Michael Kennedy: more of the requests being processed by an ASGI server
- 37:19Michael Kennedy: with like truly async calls
- 37:21Michael Kennedy: and down to the database layer and so on,
- 37:23Michael Kennedy: maybe I can use fewer workers.
- 37:25Michael Kennedy: And so I like, just off of like one of my web apps,
- 37:29Michael Kennedy: I cut like 500 megs off because I could just reduce the number of workers because each one was more capable and didn't get bound up.
- 37:36Michael Kennedy: There's still this computational problem you have, but at least you're not doing like I'm waiting a second for this API endpoint, you know?
- 37:42Michael Kennedy: Yes, yes, yes.
- 37:44Michael Kennedy: I thought that was pretty interesting.
- 37:45Carlton Gibson: No, it is.
- 37:46Carlton Gibson: And it's like people, it's one reason why people run.
- 37:49Carlton Gibson: So I always run, not always, but I default to running, say, Gun & Corn with multiple workers, multiple processes as workers because it's separate and you don't get the guild contention issues.
- 38:00Carlton Gibson: But there is a great approach, depending on how much of this CPU work that you're busy doing and how the guild contention comes into play.
- 38:08Carlton Gibson: But if you run with the threaded worker, you've just got one process.
- 38:13Carlton Gibson: And you've got much less memory overhead from running one process than you have from running four.
- 38:18Carlton Gibson: Shared initialization and all that.
- 38:20Carlton Gibson: Connection pooling, everything.
- 38:22Carlton Gibson: Yeah, all of it.
- 38:23Carlton Gibson: But it's never as simple as, oh, this is the way to deploy.
- 38:27Carlton Gibson: How do you deploy your application?
- 38:29Carlton Gibson: how long you got and how many developers are we asking because we'll all give different answers
- 38:33Carlton Gibson: for all these really interesting reasons that trade off against each other yeah something i
- 38:38Michael Kennedy: would like to get your thoughts on is i feel like sometimes people say like why are we making python
- 38:43Michael Kennedy: so complicated like the magic of python was that it was simple and yet there's also another side of
- 38:49Michael Kennedy: that story where people's like well i have to switch to go because i can't i can't do this and so you
- 38:55Michael Kennedy: got to walk this tight balance of there has to be a simple story but you don't want there to not be
- 39:00Michael Kennedy: a nice upper bound or high upper bound because you're going to lose maybe your your biggest
- 39:05Michael Kennedy: proponents or your your biggest users right yeah what do you think about that um no i think look
- 39:11Carlton Gibson: python so you know i this fits my brain idea i think it's something i'm very into and one of my
- 39:17Carlton Gibson: sort of grumbles about the type about typing is that it becomes not pipe it fits your brain i mean
- 39:23Carlton Gibson: And, you know, I was told a story at PyCon Italia,
- 39:26Carlton Gibson: I showed five typing experts sat around a table trying to work out the correct type in.
- 39:32Carlton Gibson: And these are the absolute gurus, right?
- 39:34Carlton Gibson: And that's not fit your brain, is it?
- 39:38Carlton Gibson: So part of me is like, ah, on that front.
- 39:42Carlton Gibson: But we have to be performant.
- 39:44Carlton Gibson: You don't want to have to change language just because you need to increase your throughput,
- 39:49Carlton Gibson: just because you're doing a more serious application.
- 39:51Carlton Gibson: That's not good for, that wouldn't be good for long-term sustainability of the project.
- 39:56Michael Kennedy: No, it definitely wouldn't.
- 39:58Michael Kennedy: And the fact that you don't have to add typing, you don't have to write async views.
- 40:03Michael Kennedy: I think that's part of where it's really good because you can grow into that.
- 40:07Michael Kennedy: There is space to grow into.
- 40:08Carlton Gibson: There is, though, a kind of pressure to the original DEP, which added type hints to Python.
- 40:16Carlton Gibson: said, we don't intend to make type hints compulsory, even by convention. But I think there is very much
- 40:23Carlton Gibson: people coming into the Python landscape. There is a cultural pressure to adopt typing, even if perhaps
- 40:29Carlton Gibson: it's not relevant to you. It's like, oh, no, but this is how you do it. And there is a you should,
- 40:35Carlton Gibson: which perhaps doesn't respect that original promise in the dead.
- 40:40Michael Kennedy: Fair. And I think AI throws a whole spanner into it as well. And let's not go too deeply
- 40:45Michael Kennedy: down this because we can spend the next three hours on but you know if you ask an ai to write
- 40:49Michael Kennedy: python a lot of times it'll do typed python now sure without asking yes and um the big issue
- 40:55Carlton Gibson: with um ai output is can you verify it and so you know type safety any type safety that you get from
- 41:02Carlton Gibson: the type checker is going to help the ai um verify the output or help you verify the output 100 i
- 41:09Michael Kennedy: think it's a big bonus to ai as well whether yes people care about that you know that's a different
- 41:13Michael Kennedy: deal. Indeed. So what's left? Where else do people need to focus? Okay, so performance realities is,
- 41:22Carlton Gibson: you know, DB parallelism is your limit, right? So each DB connection is a ordered, like,
- 41:29Carlton Gibson: it takes, you know, it takes a query and it gets a response. And so whether you're coming,
- 41:34Carlton Gibson: whether you're doing that in threads, the way Django does it, or if you're doing it with greenlets,
- 41:39Carlton Gibson: the way some other ORMs do it, you're still limited by the size of your connection pool.
- 41:45Carlton Gibson: Again, and that's whether you're doing it with WSGI or ASCII, the limit always has
- 41:49Carlton Gibson: been the number of database connections you could hold open at a time, basically.
- 41:54Carlton Gibson: So that doesn't really change anything.
- 41:55Carlton Gibson: If you want to scale up, you need to increase the size of your connection pool or handle
- 41:59Carlton Gibson: that more efficiently.
- 42:01Carlton Gibson: That's one thing.
- 42:02Carlton Gibson: Async transactions is something that, because of the way Django's connections are tied to a thread local,
- 42:11Carlton Gibson: we're never going to have async transactions in the sense that you can call await inside an atomic block.
- 42:19Carlton Gibson: Why not? Because that await call may not be on the same thread as the connection, right?
- 42:24Carlton Gibson: So the transaction state is held on the connection, which is on a thread,
- 42:29Carlton Gibson: and if that await call handles in a different thread, you're going to be in big trouble.
- 42:32Carlton Gibson: So it's always going to be the case with Django's choice that you need to take if you want.
- 42:36Carlton Gibson: It doesn't mean you can't have transactions in async.
- 42:38Carlton Gibson: You just wrap your transaction in a single function, which you then call within a single sync to async.
- 42:47Carlton Gibson: So that's always going to be the case.
- 42:49Carlton Gibson: Then there's CPU band work.
- 42:51Carlton Gibson: Look, if you do work on CPU, you either block the event loop or you throw it to a thread.
- 42:58Carlton Gibson: And then, you know, if there's a lot of CPU work going on, you've got GIL contention that we'll talk about momentarily.
- 43:03Carlton Gibson: Or you have to throw it out to a separate process.
- 43:05Carlton Gibson: But throwing work out to a separate process is just crazily heavy.
- 43:08Carlton Gibson: You've got to bootstrap that process.
- 43:10Carlton Gibson: You've got to marshal your data.
- 43:11Carlton Gibson: You've got to work out returns.
- 43:13Carlton Gibson: So, you know, async frameworks have tools to throw out to a separate process.
- 43:17Carlton Gibson: But it's not something you want to address lightly.
- 43:19Carlton Gibson: Really, really, threading was the way you wanted to go.
- 43:22Michael Kennedy: But you are going to hit the gill, essentially.
- 43:25Michael Kennedy: Yeah, yeah.
- 43:26Michael Kennedy: For people who don't know, if by default, when you use async and await on a standard event loop, that's single threaded.
- 43:35Michael Kennedy: Even though stuff is happening, it's literally single threaded.
- 43:38Michael Kennedy: And the way that it time slices it or preemptively shares it is anytime you hit an await block, like on a database call or whatever, it's like, okay, that's waiting.
- 43:48Michael Kennedy: So now the same thread can go process something else.
- 43:51Michael Kennedy: But soon as you're back in regular Python, just jamming on something, well, there's no way to time slice that, right?
- 43:58Michael Kennedy: So everything else is waiting on that computationally.
- 44:00Carlton Gibson: And you end up with really horrible things.
- 44:03Carlton Gibson: And I'm going to say it's horrible.
- 44:04Carlton Gibson: I think it really is horrible, right?
- 44:05Carlton Gibson: It's where you've got a loop of CPU-heavy work.
- 44:08Carlton Gibson: I know you're rendering it and you add in an asyncio sleep zero just to release work.
- 44:14Carlton Gibson: And it's like, why are we doing that?
- 44:15Carlton Gibson: We're doing that because we're running CPU work on the event loop.
- 44:19Carlton Gibson: The event loop wasn't for that.
- 44:21Carlton Gibson: It was for our handling I.O., by async I.O.
- 44:25Carlton Gibson: And we really want to throw CPU bound work to a thread.
- 44:30Carlton Gibson: We just want to.
- 44:31Carlton Gibson: But then we've got Python's guild.
- 44:32Carlton Gibson: So this is where I'm super excited.
- 44:34Michael Kennedy: It effectively says, well, you have as many threads as you want, but you still can run
- 44:37Michael Kennedy: one at a time.
- 44:39Carlton Gibson: Yeah, exactly.
- 44:40Carlton Gibson: Either way.
- 44:41Carlton Gibson: So either you do the CPU work on your one thread, which is running the event loop, or you throw
- 44:46Carlton Gibson: it out to different threads and they get GIL contention and so you end up with essentially
- 44:51Carlton Gibson: one cpu or one threads worth of clock time no matter which way you cut it which is python's
- 44:57Carlton Gibson: real problem and it has you know it it's not actually about switching threads it's not actually
- 45:02Carlton Gibson: about creating threads it's not actually about spinning up event loops those are relatively quick
- 45:06Carlton Gibson: things it's about the inability to genuinely run parallel work in in multiple threads in python up
- 45:13Michael Kennedy: until very recently. Yeah, it's another axis of that. You probably don't need it, but it's a low
- 45:19Michael Kennedy: upper bound that shouldn't be there for people who need to grow into it. Yes, entirely. And you
- 45:25Carlton Gibson: shouldn't need it. It can come in at quite moderate scales. Okay, my Hello World blog application,
- 45:33Carlton Gibson: I'm not going to hit gil contention with my 10 requests a minute. It's not a problem if I'm lucky.
- 45:39Carlton Gibson: Yeah, that's when it gets popular, yeah, in the article.
- 45:42Carlton Gibson: Yeah, yeah, yeah.
- 45:43Carlton Gibson: Oh, I hit Hacker News.
- 45:44Carlton Gibson: I've got 10 requests in a minute.
- 45:45Carlton Gibson: You know, you're not going to hit GitGil contention there.
- 45:49Carlton Gibson: But if you're, again, serious about using Python, a bigger application,
- 45:54Carlton Gibson: yes, you are going to have to do CPU work at some point.
- 45:56Carlton Gibson: Otherwise, the computer's not doing anything, right?
- 45:58Carlton Gibson: The program, if it's going to do something,
- 46:00Carlton Gibson: it's going to have to use the CPU at some point.
- 46:02Carlton Gibson: And so if you're doing that at scale,
- 46:04Carlton Gibson: the Git has been an impediment to that.
- 46:07Carlton Gibson: And people are, oh, I'm going to switch to Rast.
- 46:08Carlton Gibson: I'm going to switch to Go.
- 46:09Carlton Gibson: So do we have to switch language again?
- 46:12Carlton Gibson: Can we not do that?
- 46:13Michael Kennedy: Yeah, let's make it so we don't have to.
- 46:15Michael Kennedy: And you don't have to have an insane amount of requests
- 46:18Michael Kennedy: to benefit from this.
- 46:19Michael Kennedy: Kind of like my little example I just showed with the memory thing.
- 46:22Michael Kennedy: If you have to run enough workers that you needed two servers,
- 46:26Michael Kennedy: if this free threading thing and the async stuff
- 46:31Michael Kennedy: let you shrink that back down to one more manageable server,
- 46:34Michael Kennedy: it saves you on DevOps.
- 46:36Michael Kennedy: It saves you on complexity.
- 46:37Michael Kennedy: It saves your money.
- 46:38Michael Kennedy: It saves your money.
- 46:38Michael Kennedy: So if you want money, all these things.
- 46:41Michael Kennedy: So you don't have to be Instagram level of Django requests
- 46:46Michael Kennedy: to benefit from some of these ideas, right?
- 46:48Michael Kennedy: No, absolutely not.
- 46:49Carlton Gibson: I mean, it turns out, and I've always said for years,
- 46:54Carlton Gibson: with a medium-sized box, you can run a very high-traffic server
- 47:00Carlton Gibson: in realistic terms.
- 47:02Carlton Gibson: But the more you can do on the smaller box, absolutely the better.
- 47:06Carlton Gibson: Absolutely.
- 47:07Carlton Gibson: So three threading is the big win.
- 47:10Carlton Gibson: It's the big exciting thing because Django kind of looked clumsy almost when it said,
- 47:15Carlton Gibson: no, let's just put all the sync work into a thread.
- 47:18Carlton Gibson: But actually, as three threading becomes more established and it works through the ecosystem,
- 47:26Carlton Gibson: it looks as if Django's bet's going to pay off.
- 47:29Carlton Gibson: That bet will have aged very well.
- 47:31Carlton Gibson: Because we always really wanted to do CPU work in the thread.
- 47:36Carlton Gibson: I think it's great.
- 47:37Michael Kennedy: Yeah, I mean, you can have a thread pool.
- 47:40Michael Kennedy: Your threads could each have sort of a hanging around database connection for any time you need it, ready to go.
- 47:46Michael Kennedy: And yeah, you just delegate it out and you actually get parallelism.
- 47:50Michael Kennedy: Yeah, no, and that's the thing.
- 47:52Carlton Gibson: Actual parallelism within Python is really exciting.
- 47:56Carlton Gibson: And that's for everybody.
- 47:57Carlton Gibson: I mean, you know, to go back, why did we update the async topic docs?
- 48:01Carlton Gibson: It's because there are problems that remain with async, but they're not Django-specific problems.
- 48:06Carlton Gibson: The same GIL contention problems you have, you type FastAPI concurrency in, or scaling problems, and you read the same sort of problems.
- 48:15Carlton Gibson: And so it's not Django specific.
- 48:17Carlton Gibson: It's not a Django problem there.
- 48:20Carlton Gibson: It's a Python structural problem.
- 48:22Carlton Gibson: Same database scaling.
- 48:23Carlton Gibson: How do we do it?
- 48:24Carlton Gibson: What do database pools?
- 48:25Carlton Gibson: Well, same using Flask.
- 48:27Carlton Gibson: You're going to scale it that way, et cetera.
- 48:29Carlton Gibson: It's nice that these are general problems that we all share, I think.
- 48:33Michael Kennedy: Yes, absolutely.
- 48:34Michael Kennedy: yeah i mean and let's see i think let's open it no there we go i think it's just so easy these days
- 48:42Michael Kennedy: to try out free threaded python too it used to be oh you got to get this weird thing and build it or
- 48:48Michael Kennedy: whatever but now with uv it's just literally uv python install like what do you want do you want
- 48:54Michael Kennedy: three fourteen six t yeah that's all you're saying in two seconds you've got free threaded
- 49:00Michael Kennedy: Python to play with and try out.
- 49:02Carlton Gibson: Yes.
- 49:03Carlton Gibson: The thing here, there's still work to be done here.
- 49:06Carlton Gibson: Most of it is about the ecosystem, not about the architecture.
- 49:10Carlton Gibson: The architecture is already in Plex.
- 49:12Carlton Gibson: So if you're at an app level, because people have been running threaded Django for a long
- 49:19Carlton Gibson: time.
- 49:19Carlton Gibson: We're talking about people deploying Whiskey with threaded workers.
- 49:23Carlton Gibson: They've been doing that for a long time.
- 49:25Carlton Gibson: But standardly, at the app level, you don't touch things which are multi-thread.
- 49:30Carlton Gibson: You might have, I don't know, some module level caching.
- 49:33Carlton Gibson: You might have a property at the module level.
- 49:35Carlton Gibson: Well, hang on.
- 49:36Carlton Gibson: Is that really thread safe?
- 49:37Carlton Gibson: Do you need to wrap that in a lock?
- 49:38Carlton Gibson: But it's extensions, it's libraries.
- 49:42Carlton Gibson: Are they doing these kind of patterns?
- 49:43Carlton Gibson: And we just need to work through them and run all our test suites against them
- 49:46Carlton Gibson: and make sure that we're compatible with re-threading.
- 49:49Carlton Gibson: And then I think we're actually going to unlock an awful lot of performance for everybody.
- 49:55Carlton Gibson: So instead of having to run multiple processes to get parallelism,
- 49:58Carlton Gibson: you'll be able to run it all within one process.
- 50:01Michael Kennedy: You know, it's a really exciting thing.
- 50:03Michael Kennedy: Yeah, I have some really interesting experiments
- 50:05Michael Kennedy: I want to put together with free threading,
- 50:06Michael Kennedy: but the more important work is still out there.
- 50:09Michael Kennedy: Yeah.
- 50:10Michael Kennedy: With AI, I'm not ready to, like, go and work on these.
- 50:12Michael Kennedy: Like, I don't, I can't do another open source project yet.
- 50:15Carlton Gibson: Yeah, no, and you must avoid it
- 50:17Carlton Gibson: because the trouble is that,
- 50:19Carlton Gibson: I maintain a lot of packages, right?
- 50:21Carlton Gibson: And it's a burden.
- 50:23Carlton Gibson: It's a thing that once you've taken it on,
- 50:25Carlton Gibson: you can't just put that.
- 50:26Carlton Gibson: And you might think, oh, it's just a casual thing.
- 50:28Carlton Gibson: must be very conscious and careful about what you package up and put out there in the world because
- 50:32Michael Kennedy: you're making a promise to people like yeah carlton i'm thinking this is going to be a really good gist
- 50:38Michael Kennedy: not a repo you know what i mean yes right exactly put up a gist do a blog post put up a gist say
- 50:43Carlton Gibson: here it is but don't put it up as a as a package and unless you mean it if you mean it then it's a
- 50:49Carlton Gibson: package on pypi and you know all the rest people can install pip installs directly from github
- 50:53Carlton Gibson: You don't have often version control.
- 50:55Carlton Gibson: You don't have to put it on PyPI for people to be able to play with it.
- 50:59Carlton Gibson: Yeah, absolutely.
- 51:00Michael Kennedy: So one thing I want to talk to you about, I know you're excited about, is the built-in
- 51:04Michael Kennedy: task framework.
- 51:05Michael Kennedy: Because all of this stuff about threading and all of that, you know, parallelism and, you
- 51:11Michael Kennedy: know, event loops, it's fine.
- 51:13Michael Kennedy: But if you could just say, let's just run that somewhere else.
- 51:17Michael Kennedy: Don't hassle me.
- 51:19Michael Kennedy: Tell us about this.
- 51:19Carlton Gibson: Okay.
- 51:20Carlton Gibson: So this is a great question, because when we first added async to Django, people were like, oh, this is background tasks.
- 51:26Carlton Gibson: And it's like, no, no, no, it's not.
- 51:27Carlton Gibson: It's all about I.O. and multiple connections and long-lived requests and things like that.
- 51:32Carlton Gibson: It's nothing to do with having a queue where you want to do some asynchronous task.
- 51:37Carlton Gibson: So asynchronous is the word has appeared, but it means other than making people wait.
- 51:43Carlton Gibson: So the standard web, you've got a request and you send a response, and you want that to be as quick as you can.
- 51:49Carlton Gibson: But let's say somebody has a password reset and you want to send them an email so that they can click on the password reset link and they can reset their password.
- 51:57Carlton Gibson: Well, historically, you would have made the request, sent the email, wait for the email to send, which can take almost any amount of time.
- 52:06Carlton Gibson: And then only then do they get the HTTP response.
- 52:09Carlton Gibson: Well, that's a long time to wait.
- 52:11Carlton Gibson: That's a bad user experience, right?
- 52:12Carlton Gibson: The much better is to make the request, say, yes, we've accepted it, put it, we'll talk about where we put it in a moment, and just send the response quickly saying, look, it's on its way.
- 52:20Carlton Gibson: Give it a few seconds to arrive.
- 52:21Michael Kennedy: It's already email.
- 52:23Michael Kennedy: It's already a single.
- 52:23Michael Kennedy: There's nothing you can do to wait for delivery.
- 52:26Carlton Gibson: Yeah, yeah, yeah.
- 52:26Carlton Gibson: So all you can do, though, is send.
- 52:28Carlton Gibson: So what you do now is you quickly write a task to, say, the database with the Django task framework.
- 52:34Carlton Gibson: And you would send that, there'd be a worker process running totally separately, which could then send out the email, say, for instance.
- 52:42Carlton Gibson: Now, people have historically done this with either Celery or RQ or Django Q or then Django Q2.
- 52:49Carlton Gibson: And there are Huey, there's a billion of these task queues out there.
- 52:53Carlton Gibson: And problem there is that, say, Django itself couldn't rely on them.
- 52:58Carlton Gibson: And neither could any third party package rely on them.
- 53:01Carlton Gibson: Well, I could, but then I'd have to commit to, we're using Django Q2 for this.
- 53:05Carlton Gibson: And all the people who were using Celery or RQ or Huey, they'd be excluded.
- 53:09Carlton Gibson: And so what Django Tasks does is to provide a kind of back-end API that says, look, any package can use this API to enqueue a task and to check results and do the core activities that you need to do around asynchronous tasks.
- 53:27Carlton Gibson: And then just by the user, just by configuring in the settings what their back end is, can either go for Celery or RQ or it depends.
- 53:35Carlton Gibson: As long as those packages have implemented a Django tasks adapter, you can use those for the back end.
- 53:42Carlton Gibson: And so I think, for instance, Celery hasn't yet done it, but there's an open issue and it will just it will become.
- 53:47Carlton Gibson: But there are a couple of back ends that are available.
- 53:49Carlton Gibson: There's a Redis one, there's one that uses the ORM to just store in your database and you can get going with those now.
- 53:55Carlton Gibson: And for me as a third-party package maintainer, that's fantastic because now I can add tasks to my third-party package and users can benefit from those without me having to know anything about which backend they're using.
- 54:06Carlton Gibson: Yeah.
- 54:06Michael Kennedy: Yeah.
- 54:07Michael Kennedy: Your pip install or uv install instructions for your package aren't like bracket celery, bracket, whatever.
- 54:15Michael Kennedy: And if none of those are there, it's going to throw an acceptance.
- 54:18Michael Kennedy: Well, you didn't install any of the queuing options.
- 54:20Michael Kennedy: So now we're busted, you know, like, yeah, you can just delegate.
- 54:23Michael Kennedy: Yeah.
- 54:24Carlton Gibson: And Cellerie's wonderful.
- 54:26Carlton Gibson: It really is a truly wonderful piece of software.
- 54:29Carlton Gibson: But it's literally overkill for 99% of projects.
- 54:33Carlton Gibson: And it's operationally difficult to keep going.
- 54:36Carlton Gibson: You end up running three or four different things.
- 54:37Carlton Gibson: You need a monitor, a beat server, the actual worker.
- 54:43Carlton Gibson: It's designed for a level of sophistication that most applications really don't need.
- 54:48Michael Kennedy: I love that you call out the operational complexity as well.
- 54:53Michael Kennedy: Because like you say, it would be nice to just let people who don't have something complicated use this feature without going, yeah, look, I know you used to, look, that was fun when you could just put it on a platform as a service thing and let it run.
- 55:05Michael Kennedy: But now what we're doing is we're setting up a private network and now we've got poison messages and we've got downtime because the celery version had to change.
- 55:14Michael Kennedy: Oh my gosh.
- 55:16Michael Kennedy: It's a step jump, right?
- 55:17Michael Kennedy: And just to be able to avoid that is great.
- 55:20Carlton Gibson: No, it's a serious, serious.
- 55:22Carlton Gibson: If you are taking on celery, then there is a serious question to ask.
- 55:28Carlton Gibson: You know, if you're an established team, but if you're growing and suddenly it's like, shall we add celery?
- 55:34Carlton Gibson: It's like, well, hang on, look at your ops capacity and look at your ability to, you know, do you have someone on call for when the workers break down at two o'clock in the morning and page notifications and all those things?
- 55:44Carlton Gibson: Because it's a world up from self-hosting, you know, your bog standard Django app.
- 55:51Michael Kennedy: As someone who does a lot of DevOps without a lot of support, I hear that.
- 55:56Michael Kennedy: So, you know, Carl, I think this could have been like a three-hour show easily, I'm starting to realize.
- 56:03Michael Kennedy: But let's sort of get some advice to kind of wrap things up.
- 56:07Michael Kennedy: We're on a glide path here for people.
- 56:09Michael Kennedy: Should they write async views today?
- 56:11Michael Kennedy: What do you think about this?
- 56:14Michael Kennedy: Yes, right.
- 56:14Carlton Gibson: So if I'm writing a small application, like just my local blog application, I want to add a little bit of async sprinkle to it.
- 56:21Carlton Gibson: I might just deploy a single ASGI worker by itself.
- 56:24Carlton Gibson: It's probably big enough.
- 56:26Carlton Gibson: You're not going to hit scaling problems, you know, with low traffic.
- 56:28Carlton Gibson: So, you know, just a single ASGI application.
- 56:31Carlton Gibson: I think the, as I say, the people who are doing this at a bigger level than that, I think they're running their core application as WSGI and the standard ways because the scaling patterns there are so established and known.
- 56:42Carlton Gibson: and it's almost trivial by comparison.
- 56:45Carlton Gibson: But then they're running a little sidecar
- 56:47Carlton Gibson: to handle their app.
- 56:49Carlton Gibson: So just have, you know,
- 56:50Carlton Gibson: engine XA can route by the path
- 56:52Carlton Gibson: or it can route by other things.
- 56:53Carlton Gibson: But, you know,
- 56:54Carlton Gibson: if it's got connection header,
- 56:55Carlton Gibson: keep route,
- 56:55Carlton Gibson: but that's a little advanced.
- 56:56Carlton Gibson: Just route by path,
- 56:57Carlton Gibson: put everything under an A prefix.
- 56:59Carlton Gibson: And then, right,
- 57:00Carlton Gibson: those are your async ones for,
- 57:02Carlton Gibson: I don't know,
- 57:02Carlton Gibson: your long-lib service center events requests.
- 57:05Carlton Gibson: And then send everything else back
- 57:06Carlton Gibson: by your whiskey.
- 57:06Carlton Gibson: I mean, just do that.
- 57:07Carlton Gibson: Get the hang of it.
- 57:08Carlton Gibson: You'll find the scaling,
- 57:10Carlton Gibson: that scales very well.
- 57:11Carlton Gibson: And then you can say,
- 57:12Carlton Gibson: okay, well, now if I move this to async, oh, okay, I start to see some of the complications of async appear.
- 57:17Carlton Gibson: And I think that's the danger I see is when people dive feet first into async without really understanding
- 57:24Carlton Gibson: that it is an order of magnitude more complicated to correctly handle async code than to just code synchronously.
- 57:32Carlton Gibson: For all these reasons about blocking the event loop and you get deadlocks.
- 57:37Carlton Gibson: These are really complex patterns that grow into them rather than jumping headphones.
- 57:41Michael Kennedy: yeah yeah but django is ready yes no django's async story is there the problems that i see come up
- 57:47Carlton Gibson: time and time and time again are problems with python and python's async story that we all share
- 57:53Carlton Gibson: not django specific problems yeah or even that asynchronous programming in general yes asynchronous
- 57:58Carlton Gibson: yeah they are yeah they are async or guild problems not um django specific django specific
- 58:06Michael Kennedy: problems. Absolutely. I want to just put this out in the universe. If you're looking at your web app
- 58:12Michael Kennedy: and you're like, ah, these are a little bit slow, these requests. So we're going to need to set up
- 58:17Michael Kennedy: some parallelism so that we can handle multiple slow requests at a time. Like just take an hour
- 58:22Michael Kennedy: and look at your database indexes, please. Database indexes are magic. And I'm not saying it's not a
- 58:29Michael Kennedy: benefit to go async or whatever, but if slow is a problem, please just do, you know, look at the
- 58:35Michael Kennedy: query plans, look and make sure every hot path is using a database index and so on.
- 58:40Michael Kennedy: Yes, yes, exactly.
- 58:42Carlton Gibson: I mean, and if it, so when you make a database, we always talk about the database, right?
- 58:46Carlton Gibson: But if Django makes a database request, there's a little bit of CPU time to compile the request.
- 58:52Carlton Gibson: And then there's the IO bit where it's talking to the database.
- 58:55Carlton Gibson: And, you know, PsychoPG already releases the gil in that context.
- 58:59Carlton Gibson: So you're already getting, you know, what concurrency or parallelism you would have at that point.
- 59:04Carlton Gibson: And then there's CPU time to sort of marshal the return into,
- 59:09Carlton Gibson: or structure the returned data into ORM objects.
- 59:13Carlton Gibson: So those bits are CPU bound.
- 59:15Carlton Gibson: But that core bit is already releasing the gil.
- 59:18Carlton Gibson: You're already going to get parallelism there.
- 59:20Carlton Gibson: So the chances are you're not going to get much more
- 59:24Carlton Gibson: by jumping to Async or adding more workers.
- 59:26Carlton Gibson: It was your database all along that was the thing that was taking the time.
- 59:30Michael Kennedy: These things that we've been talking about,
- 59:32Carlton Gibson: about CPU contention and all the rest.
- 59:34Carlton Gibson: It's, ah, they come up in only specialized cases.
- 59:38Carlton Gibson: They're kind of advanced topics, if that makes sense.
- 59:42Carlton Gibson: They're often not the problem.
- 59:44Carlton Gibson: Amazing when you need them, but it's not a panacea, yeah.
- 59:47Carlton Gibson: Yeah, and we, I mean, how would I phrase it?
- 59:50Carlton Gibson: We spend so much time thinking about the use cases
- 59:53Carlton Gibson: for the advanced cases that we sometimes forget
- 59:56Carlton Gibson: that the simpler ones are much more simple stories.
- 59:58Carlton Gibson: And yeah, should I be writing async views?
- 1:00:00Carlton Gibson: well, do it gently.
- 1:00:03Carlton Gibson: Don't jump into it thinking it's some cure-all.
- 1:00:06Carlton Gibson: Yeah, but Django's ready.
- 1:00:07Carlton Gibson: That's cool.
- 1:00:08Carlton Gibson: Yeah, yeah, yeah.
- 1:00:08Carlton Gibson: No, absolutely.
- 1:00:09Carlton Gibson: And that's why we made the effort to update the topic,
- 1:00:14Carlton Gibson: because we realized we were giving users bad advice.
- 1:00:17Carlton Gibson: Yeah, you're scaring them off.
- 1:00:19Carlton Gibson: Yeah, no, we really were.
- 1:00:20Carlton Gibson: We were like, oh, it's still incomplete.
- 1:00:22Carlton Gibson: It's not ready.
- 1:00:22Carlton Gibson: No, it's there.
- 1:00:24Carlton Gibson: And yes, you will run into problems as you scale up,
- 1:00:27Carlton Gibson: but that's not Django.
- 1:00:28Carlton Gibson: That's async.
- 1:00:29Michael Kennedy: Yeah, indeed.
- 1:00:30Michael Kennedy: Final call to action.
- 1:00:31Michael Kennedy: People consider Django.
- 1:00:33Michael Kennedy: Maybe we're considering async for their existing Django projects
- 1:00:37Michael Kennedy: or other things like the Django tasks, background tasks and so on.
- 1:00:40Michael Kennedy: What do you tell them?
- 1:00:41Carlton Gibson: Yeah, I mean, I would get going with Django tasks.
- 1:00:44Carlton Gibson: You install the database backend for that,
- 1:00:47Carlton Gibson: which is all the Redis ones work perfectly as well.
- 1:00:49Carlton Gibson: If you've already got Redis in your stack
- 1:00:51Carlton Gibson: and you want a little bit more,
- 1:00:53Carlton Gibson: but most people don't need that.
- 1:00:55Carlton Gibson: Just use the ORM database queue.
- 1:00:57Carlton Gibson: It's a great storage.
- 1:00:58Carlton Gibson: That's where you want to throw most of your work.
- 1:01:01Carlton Gibson: You don't really want to be...
- 1:01:03Carlton Gibson: With Async.io, you can, yes, throw something off to an executor.
- 1:01:08Carlton Gibson: No, but don't do that because you've got no queue controls.
- 1:01:11Carlton Gibson: You've got no result.
- 1:01:12Carlton Gibson: You can't say, did it fail and then have logic to retry,
- 1:01:15Carlton Gibson: any of those sorts of things.
- 1:01:17Carlton Gibson: It's just running on the event loop.
- 1:01:19Carlton Gibson: And if it crashes, well, what do you do about that?
- 1:01:21Carlton Gibson: How do you recover from that?
- 1:01:22Carlton Gibson: There's no elegance to that.
- 1:01:23Carlton Gibson: So, yes, Django tasks is probably where I would point people first.
- 1:01:27Carlton Gibson: And then do I need long-lived requests?
- 1:01:29Carlton Gibson: Do I want to do things like service and events or WebSockets?
- 1:01:31Carlton Gibson: Yeah, that's where.
- 1:01:32Carlton Gibson: Or streaming, streaming responses.
- 1:01:35Carlton Gibson: Async, great, great example.
- 1:01:37Carlton Gibson: Yeah.
- 1:01:37Michael Kennedy: You want to adopt Datastar or one of these frameworks that really leverages service and events.
- 1:01:42Michael Kennedy: Yeah.
- 1:01:42Michael Kennedy: Yeah.
- 1:01:43Carlton Gibson: Or HTMX version 4, which is in beta whatever now, which I think is the risk.
- 1:01:48Michael Kennedy: Oh, is that coming with that as well?
- 1:01:50Carlton Gibson: Yeah, yeah, yeah.
- 1:01:51Carlton Gibson: So they basically were like, right, it's okay.
- 1:01:53Carlton Gibson: So I think Carson was like, we're never going to do an HTMX version 3.
- 1:01:57Carlton Gibson: and he's kept his promise because he's jumped straight from two to four um but the it
- 1:02:02Carlton Gibson: was um it was inspired by some of the things from um data star and the surrounding projects it's
- 1:02:07Michael Kennedy: like hang on we can have a go at that as well i think said goss you have to get him on and he can
- 1:02:11Michael Kennedy: talk you through it yeah i i love carson i think what he's doing is great and i've had him on before
- 1:02:15Michael Kennedy: i'm a fan of hdmx so that's cool maybe we'll do another no other round yeah so when i stepped
- 1:02:20Carlton Gibson: down as a fellow in 23 i started a new application been building with hdmx and alpine since day one
- 1:02:26Carlton Gibson: and just absolutely adore it.
- 1:02:29Carlton Gibson: It's combined with template partials,
- 1:02:30Carlton Gibson: which we added into Django,
- 1:02:31Carlton Gibson: it's really changed the way I write web applications.
- 1:02:34Carlton Gibson: It's amazing.
- 1:02:34Carlton Gibson: All for the better as well.
- 1:02:35Michael Kennedy: It's like JavaScript magic fairy dust.
- 1:02:38Michael Kennedy: Yeah, it's how it should be.
- 1:02:40Michael Kennedy: Yeah, exactly.
- 1:02:41Michael Kennedy: Sprinkle it on there.
- 1:02:42Michael Kennedy: It doesn't have to be a spa.
- 1:02:43Michael Kennedy: Everyone calm down.
- 1:02:44Michael Kennedy: We could still do some stuff on templates and so on.
- 1:02:47Carlton Gibson: But when I learned JavaScript, that was how it was.
- 1:02:51Carlton Gibson: You detach a few event listeners
- 1:02:53Carlton Gibson: and just do your little thing.
- 1:02:54Michael Kennedy: And that was enough, right?
- 1:02:56Michael Kennedy: hashtag dollar document ready.
- 1:02:58Michael Kennedy: Let's go.
- 1:03:00Michael Kennedy: A little jQuery back in the day, right?
- 1:03:02Michael Kennedy: Yeah.
- 1:03:03Michael Kennedy: That's how it was.
- 1:03:04Michael Kennedy: Yeah, yeah.
- 1:03:04Michael Kennedy: Cool.
- 1:03:04Michael Kennedy: Select something, do something.
- 1:03:06Michael Kennedy: Exactly.
- 1:03:07Michael Kennedy: Carlton, thanks for first all the stuff on Django for everybody
- 1:03:10Michael Kennedy: and second, coming on the show to share.
- 1:03:12Carlton Gibson: No, thanks.
- 1:03:12Carlton Gibson: Thanks for the chat.
- 1:03:14Carlton Gibson: It's been really good.
- 1:03:14Michael Kennedy: It's really lovely because you pick on the topics
- 1:03:18Carlton Gibson: and I'm like, yeah, yeah, this is right.
- 1:03:19Michael Kennedy: Thanks.
- 1:03:20Michael Kennedy: See you later.
- 1:03:20Michael Kennedy: Thank you, Michael.
- 1:03:21Michael Kennedy: This has been another episode of Talk Python To Me.
- 1:03:24Michael Kennedy: Thank you to our sponsors.
- 1:03:25Michael Kennedy: Be sure to check out what they're offering.
- 1:03:26Michael Kennedy: It really helps support the show.
- 1:03:28Michael Kennedy: Take some stress out of your life.
- 1:03:30Michael Kennedy: Get notified immediately about errors and performance issues
- 1:03:34Michael Kennedy: in your web or mobile applications with Sentry.
- 1:03:36Michael Kennedy: Just visit talkpython.fm/sentry and get started for free.
- 1:03:41Michael Kennedy: Be sure to use our code, talkpython26.
- 1:03:44Michael Kennedy: That's Talk Python, the numbers two, six, all one word.
- 1:03:48Michael Kennedy: If you or your team needs to learn Python,
- 1:03:51Michael Kennedy: we have over 270 hours of beginner and advanced courses
- 1:03:54Michael Kennedy: on topics ranging from complete beginners to async code, Flask, Django, HTMX, and even LLMs.
- 1:04:01Michael Kennedy: Best of all, there's no subscription in sight.
- 1:04:04Michael Kennedy: Browse the catalog at talkpython.fm.
- 1:04:06Michael Kennedy: And if you're not already subscribed to the show on your favorite podcast player,
- 1:04:10Michael Kennedy: what are you waiting for?
- 1:04:12Michael Kennedy: Just search for Python in your podcast player.
- 1:04:13Michael Kennedy: We should be right at the top.
- 1:04:15Michael Kennedy: If you enjoy that geeky rap song, you can download the full track.
- 1:04:18Michael Kennedy: The link is actually in your podcast blur show notes.
- 1:04:20Michael Kennedy: This is your host, Michael Kennedy.
- 1:04:22Michael Kennedy: Thank you so much for listening.
- 1:04:23Michael Kennedy: I really appreciate it.
- 1:04:25Michael Kennedy: I'll see you next time.
- 1:04:46We tapped into that modern vibe overcame each storm.
- 1:04:50Talk Python To Me.
- 1:04:52async is the norm.
- 1:05:22.