Latest / Talk Python To Me / #98: Adding concurrency to Django with Django Channels
Transcript
- 0:00One of the major areas of innovation in Python, especially Python 3, is advances in async and
- 0:06concurrent programming. Yet when working with any of the major web frameworks, Django, Flask,
- 0:11or Pyramid, there is basically no concurrency option. That's why Andrew Godin decided to tackle
- 0:17the issue on the Django side with his project Django Channels. This is Talk Python To Me,
- 0:22episode 98, recorded January 17, 2017.
- 0:26Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the
- 0:55ecosystem, and the personalities. This is your host, Michael Kennedy. Follow me on Twitter
- 1:00where I'm @mkennedy. Keep up with the show and listen to past episodes at talkpython.fm
- 1:05and follow the show on Twitter via at Talk Python. This episode is brought to you by Metis and
- 1:12Hired. Thank both of them for supporting the show by checking out what they have to offer
- 1:16during their segments. Andrew, welcome to Talk Python.
- 1:19Thank you. It's very nice to be here.
- 1:21Yeah, I'm excited to have another Django topic on the show. I don't cover Django that
- 1:26often and people really love it. And I'm excited to sort of mix together the worlds of asynchronous
- 1:31programming and Django and this cool project that you're working on called Django Channels.
- 1:36Yeah, I'm excited to talk about it too. Like it's combining one of the hot new things in
- 1:40Async with one of the boring old things in Django, one might say. But yeah, it's going to be a good
- 1:45thing to talk about. Hopefully people can learn some stuff from this.
- 1:48Yeah, I'm sure they will. I'm sure they will. And before we dig into that though, let's hear
- 1:52your story. How do you get into programming?
- 1:53So I have sort of those almost cliche stories. Like I got into programming about the age of 14
- 1:59or 13 or so. Just like sort of the young sort of programmer model. I started off weirdly on a,
- 2:07I think it was a Palm Pilot 3 X. Just writing in like sort of simple basic program running small
- 2:14games. And that quickly went into like doing JavaScript and websites and then doing PHP.
- 2:18And then finally about, about 10 years ago now going over into doing Python. And I've been doing
- 2:24Python ever since pretty much.
- 2:25That's really cool. What language did you program the Palm Pilot thing in?
- 2:28It was like a weird variant of basic. Like I need to go back and like find the actual program,
- 2:33like posterity, but it was like a very simple basic, had like basic graphical stuff and audio
- 2:38stuff. Like a simple breakout clone, a sort of touchscreen keyboard, a few things like games
- 2:46and interactivity has always been a big part of my attraction to programming. So like that's,
- 2:49that's why I started out doing definitely. And that's kind of almost still where I do a lot
- 2:54of my hobby stuff outside of web stuff.
- 2:56Oh yeah, that's cool. Games, games are definitely fun. Although they're both easier and harder
- 3:01these days, right? The programming languages are better. The tooling is better. But if you're
- 3:05going to make a serious game, like that's a, that's a team of people for a year, right? Like
- 3:10it, it's not just, you can throw out something really simple and it'll sort of compete.
- 3:14Oh, like starting a game is super easy. Finishing and polishing one is the hardest thing in the world.
- 3:20Like I've, I've released one of them. It took me like almost two years for a simple puzzle game.
- 3:25So I'm very aware of how hard it is. I have a lot of respect for those teams that make actual
- 3:29proper games.
- 3:30I do as well. Absolutely. I worked on some 3d simulators and stuff for a while and
- 3:35after that, I'm like, okay, this is really interesting and it's probably not something
- 3:39I want to do anymore. So, so you said you got into Python about 10 years ago. Like what was the,
- 3:46how do you get introduced to it?
- 3:47Oh, it's all a bit hazy now. So I know in particular that, actually it was more than 10 years ago.
- 3:53It's about 12 years ago now. So that the first thing I ran into, like I was running PHP apps at the
- 3:58time for, you know, for about three or four years before this. And then I ran into turbo gears and turbo gears
- 4:03back then was just sort of a couple of other packages glued together with extra stuff.
- 4:07And that was my introduction to web programming in Python in particular. Like I was looking for an
- 4:12avenue away from PHP and you know, the number of security vulnerabilities, even in my first open
- 4:17source product back then was astonishing, including the ability to read the ETC password file. So that
- 4:23was fun.
- 4:24So I was like looking, I was looking for a sort of alternate language and I think Python
- 4:29cropped up as like a common suggestion and turbo gears was one of the big things at that point.
- 4:32Django was around, but very young and new as well. And so I sort of started out doing turbo gears for,
- 4:38for a couple of years. And then I had the opportunity to go and work at an agency with Simon Willison,
- 4:44who's one of the co-creators of Django. And it is those four or five short weeks with Simon,
- 4:51who is, if you ever meet him, one of the most enthusiastic people in the world that converted
- 4:57me from turbo gears to Django by almost just force of personality. And I've been doing Django pretty
- 5:02much ever since.
- 5:03Oh, that's really great. It's, it's lovely to be around people that are so excited about technology.
- 5:07It just, it draws you in, doesn't it?
- 5:10Oh yeah. I learned so much. Like I basically pair programmed with him for like,
- 5:13you know, three weeks continuously almost. And I just learned so much both about like,
- 5:18not only like how Django works and how Python works in general, but also like a lot of the
- 5:22philosophies of like, you know, things like the Zen of Python that you don't necessarily learn going
- 5:26into the language per se, but you sort of pick up along the way. So that's the kind of stuff that's
- 5:29really valuable.
- 5:30Oh yeah, absolutely. That's really, really great.
- 5:33Let's talk about this project that you've created, Django Channels. Can you just kind
- 5:38of tell us briefly, you know, what is Django Channels?
- 5:40It's a very hard thing to describe, but I'll have a go. So Django Channels is actually,
- 5:44it's kind of two things. It's important to, as we talk about it more in the podcast.
- 5:48So first of all, so the headline feature as you were is Django Channels is a way of having
- 5:55WebSockets supported in Django. And we can, we can go in later on as to why that's difficult and hard,
- 6:01but needless to say that until Channels existed, WebSockets was very hard to do in Django just
- 6:06due to some of the technical limitations therein. But the second part of Channels is sort of this
- 6:12underlying layer of like to solve the problem in WebSockets, there's a lot of interesting
- 6:16technical challenges that go with that. And so part of it is like, well, how do we solve the problem of
- 6:22writing semi-asynchronous code, not fully asynchronous code, but semi-asynchronous code with Django and having
- 6:28things like the Django ORM and the middleware and stuff and things that people are familiar with
- 6:33still work. Like how do we keep all that stuff familiar yet also have an extra power in there?
- 6:38And so it's sort of this two-faced story of the underlying solution that powers everything.
- 6:43And then on top of it, the nice layer of, and here's WebSockets support to go on top of it.
- 6:47Okay. And just making that really easy and giving people a great API for it, huh?
- 6:52Yeah. So like, you know, the channels part, sort of the top layer is very much the let's have a nice
- 6:56design thing with a good developer experience that says, hey, you can WebSockets. And more importantly,
- 7:02it's very hard to shoot yourself in the foot or deadlock or live lock or any of the things that
- 7:08asynchronous programming gives you as like sort of loaded ammunition very easily.
- 7:13Absolutely. So before we dig into your project too much, like let's talk about how asynchronous
- 7:18programming in Django without your project works today.
- 7:22Today, Django doesn't really do nearly anything asynchronously. So like if you are at all familiar
- 7:28with Python's asynchronous solutions, Python really wasn't designed to have asynchronous programming in
- 7:35it, which is fair enough. It's a very old language. It's incredibly old at this point in some ways.
- 7:40And so especially in Django, which itself is over a decade old now and was built back when Python
- 7:462.4 was fresh and young, a lot of the ways Django is built are not built around the ideas of async.
- 7:52And in Python, you have to build with async specifically in mind and build around things
- 7:56like async, hyalur, twisted to have support for that stuff. And so before today, you just couldn't
- 8:03do it in Django. Like there's no way you could like yield and let something else run. Like if you wanted
- 8:08to do say, go and fetch a couple of web pages, you might be able to fetch those in parallel in
- 8:15one worker process, but that's as much as you could do. You couldn't share the load. And in particular,
- 8:19things like WebSockets, you would have to tie up a single Python process for every terminated socket,
- 8:26which as you can imagine, quickly wastes all of your worker processes. Like for HTTP, it's great. Like
- 8:32the process spins up, does a request. You know, if you've got a good site, like maybe two,
- 8:39300 milliseconds, finishes the request, moves on to the next request. With a socket, it holds open
- 8:44forever. And so you're quickly going to run out of those non-asynchronous worker threads. And like,
- 8:49that's kind of the problem that you butt up against very quickly.
- 8:52Yeah. You're going to run out of space really quick. If that's the case. Sorry, our website is busy. We
- 8:57have seven people on it. Could you come back later? Yes. You're in a queue. You're number five in the
- 9:03queue. Exactly. You could give them a cool little countdown. That would be nice. Oh yeah, no. You can
- 9:08make it really not a really nice experience around this really whole full internal programming you have.
- 9:12Nice. So what was the motivation or what pushed you to go and create channels? Like it's one thing to say
- 9:19Django doesn't support this ASIC model super well. It's another to go and I'm going to fix it.
- 9:24Yeah. And you think I would have learned as well. So like for this list is not familiar. My path into
- 9:30Django core team in particular was based on writing South, which was Django's sort of what one of the initial
- 9:37migration frameworks for database migrations for Django. And over the time, about eight years it took, I think,
- 9:42it became the de facto solution and then the only solution really much. And then finally it was moved into core.
- 9:49And so I had basically just finished like doing all the porting into Django core. I finally,
- 9:55like after eight years had a bit of a break. And of course I went, what other hard problem
- 10:00needs solving that I can go and look at? Like this is the awful part of my sort of like idle thinking.
- 10:06And in particular, I've been interested in WebSockets for quite a while. So like since they came on sort of the browser scene,
- 10:12well, four or five years ago now, I'm not, I can't remember the timeline perfectly. I've been looking at them
- 10:18sort of very intently. And like in particular for game related reasons, right? Like WebSockets are this
- 10:24unique thing in the web where you have very low latency, bi-directional communication with no overhead.
- 10:29like Ajax is like one way. It's got quite a lot of overhead. It's quite slow.
- 10:33And it's hard to even send things from the server to the client without prompting. So I was like, okay,
- 10:38I'm interested in this. And then back then I sort of played around a bit. I used eventlets and sort of
- 10:43kept up with the ever-changing WebSocket specification, sort of fixed eventlets as the
- 10:49spec changed a little bit and tried to keep up for it. And then, and then dropped off for a while.
- 10:53And about that time, Aymeric Augustin sort of took on the challenge of WebSockets and Django himself.
- 10:59And he made his own attempt to this where he had a different arrangement to it. And I sort of played around
- 11:04with that, like, okay, this is, this is pretty interesting. I like that.
- 11:06And there were lots of other attempts. And a lot of the common pattern was that you would,
- 11:10you would have Django running normally with WSGI. And then you would also have a separate process.
- 11:16And that separate process terminates WebSockets. And then somehow, often with either mystery or some
- 11:22kind of magic, the two processes communicate with each other. And you can hand off things from Django
- 11:27to WebSockets and vice versa. But it was always sort of bolted on. It wasn't, it wasn't really a good
- 11:31sort of like clean solution. It often didn't feel like Django as well. You know, it was,
- 11:36this extra server you ran just WebSockets. And I was like, at some point, you have to have that
- 11:40separate server idea, as we'll come to later anyway. But I wanted to sort of like, how can we make the
- 11:45solution that is more encompassing, that supports both WebSockets, and also other things that need
- 11:50the same kind of problems, like long pulling HTTP, and sort of other protocols, like incident of things
- 11:56protocols are often like this too. So how do we encompass all those problems into a common thing
- 12:00that also feels like Django integrates really well with Django?
- 12:03Right. That sounds like a great, great goal. And really definitely something to start working for
- 12:10to bring all that in there. I think one of the challenges is you want to support WebSockets,
- 12:16but you also talked about like HTTP long polling. And there's a few other styles as well.
- 12:23Some kind of like frame, like long, slow frame or something like that.
- 12:27Yeah.
- 12:28What's that called?
- 12:29There's lots of different names for it. Like, I forget. The idea in particular is that you open a,
- 12:34you have a response and you send it in the chunked encoding. You send like one chunk and then wait.
- 12:39And browsers can actually, the JavaScript doesn't actually look at chunks that come in. So you sort
- 12:44of like fake a one-way socket that way.
- 12:46Exactly. So the challenge is you've got WebSockets, which when you started this project, what year was that?
- 12:53The project was conceived in late 2014, I would say.
- 12:58But there was a long period of me playing around with prototypes and ideas and a lot of failed attempts
- 13:04and failed API versions at trying to make a good API before it sort of emerged in 2015, pretty much.
- 13:10Okay. So you didn't have too much of the problem of browsers not supporting it, but still,
- 13:16if you support WebSockets, long polling, some of the other variations, you've got to have like graceful
- 13:21fallback and negotiation and all sorts of interesting things that not just implementing pure WebSockets
- 13:29solves, right? You've got kind of a better solution than that or a broader solution.
- 13:33Well, actually, so no, so channels doesn't do fallback. And it's one of the things that's
- 13:37interesting and like one of the things we plan to do, but like, so in terms of scoping, one thing I have
- 13:41learned from South is trying to keep scope small. And in particular,
- 13:45I wanted to solve the problem of the core problem of how do we have basic async stuff in Django
- 13:51initially without having the fallback stuff. And we are working on fallback, but like in Django,
- 13:57in channels today, you can do long polling. You can do the sort of frame by frame push. You can do
- 14:04the fallback stuff. And so the fallback stuff isn't there mostly because I didn't want to reinvent the
- 14:09wheel in that regard. I wanted to reuse one of the existing solutions like Sock.js or Socket.io,
- 14:14one of the other ones that already exists in the JavaScript world. And so the initial version was,
- 14:18okay, let's build initial great support for just WebSockets, just long polling, just pushing stuff.
- 14:25And then later on, come and then join them together into a more sort of cohesive whole,
- 14:30if you will want that stuff.
- 14:31I see. So even though you support the different protocols, it's basically you're saying like,
- 14:36look, if you want to use this in your web app, put some bit of JavaScript that will select it for you.
- 14:42And we'll just stick with that one rather than having some really complicated negotiation.
- 14:47It's partially that. And partially also what I tell a lot of people is that in some ways,
- 14:52if you're just using one of the wrappers around sort of that hides all the different things it's
- 14:56transporting over, you don't have as many guarantees, right? Like if you have a wrapper,
- 15:01you don't know your underlying connection is actually good at bi-directional communication.
- 15:04It might just be a long poll Ajax call. And those are really bad at say,
- 15:08pushing data from the server to the client. So in many ways, I'm encouraging this for,
- 15:14if you really need WebSockets, like a hundred percent, like, you know, you're doing
- 15:18real time-ish kind of stuff or programming or games or whatever, or chat stuff,
- 15:22then this is the right stuff to do. Like you've got the right level of abstraction.
- 15:25If you want that sort of wrapper around stuff, sure, you can do it, but you might be better off
- 15:30just building anything long pole anyway, because then you can build to a set of guarantees that you
- 15:35can sort of understand. Right. That is the problem. If you let the system auto select
- 15:39basically different transport protocols is you don't really know what you're going to get, right?
- 15:44Yeah. And of course, WebSockets are more efficient. Like often you want to pick them
- 15:48over long polling, but in terms of scoping and trying to sort of pick what to work on,
- 15:53I pushed that stuff back in favor of just getting it working first.
- 15:56Yeah. That makes a lot of sense. So I haven't checked can I use.com for WebSockets in a while.
- 16:01What's the likelihood that people coming to my site would be able to use the best version,
- 16:06the WebSockets?
- 16:07So you have a deadly combination of it's mostly supported by all the new browsers.
- 16:11So, you know, like your new Chrome's new Firefox's I think even Edge has support for it these days,
- 16:16but you've got problems like mobile Safari, I think still doesn't support it properly.
- 16:19The old Android Chrome's probably won't do it properly either.
- 16:22And then on top of that, you have the problem where WebSockets isn't normal HTTP.
- 16:27And so a lot of proxies and corporate firewalls just block it entirely.
- 16:31And even then, if that works properly, your server and your hosting has to support HTTP 1.1
- 16:38to correctly get this protocol.
- 16:40So what happens is it starts off HTTP and then sort of upgrades itself midstream into a binding protocol
- 16:45and just binary happens in the middle of it.
- 16:47And so if you don't understand 1.1, then your HTTP server is going to go like,
- 16:52what's happening?
- 16:53And just like basically return 200 OK and break everything.
- 16:55So it's really sort of you need to trio things to work properly.
- 16:59And sometimes that can be pretty tricky to have.
- 17:02Right.
- 17:02And you don't always have great visibility, especially with the proxy servers or the firewalls.
- 17:06Yeah, the firewalls are the worst thing because like browsers, you can test in JavaScript pretty easily.
- 17:10And the server side stuff you have control over.
- 17:13But sometimes those proxies or firewalls can really sort of throw a wrench in the works.
- 17:17Oh, yeah, absolutely.
- 17:18So we talked about WebSockets and log polling.
- 17:21What are some of the other features that you support?
- 17:24There's things like custom events and tasks and a number of other things as well, right?
- 17:28This is what I mentioned in the beginning.
- 17:29Like it's not just the support for WebSockets on top.
- 17:32Like there's this whole underlying layer that's basically an event driven layer inside Django.
- 17:37And it happens that on top of that, there is WebSockets and log polling.
- 17:41But also you can use the same code for whatever you like.
- 17:44So if you want, you can have a custom set of events or custom channels that you send things to.
- 17:50You can have custom protocols, interfaces.
- 17:53So right now that people working on things like a Slack interface that sort of there are channels for when you get a Slack message into your server.
- 18:00So you can write bots very easily.
- 18:01So you can do things like if there's like 10 people on the site, one person could send a message and you could like broadcast that out to the other people who are around.
- 18:10Exactly.
- 18:10Yeah.
- 18:11And so like that in particular, the thing called groups and groups is a sort of built in primitive for almost like broadcasting to multiple and receiving channels.
- 18:20So it's like basically channels is a couple of primitives.
- 18:22Like there's a channel where you can send and receive stuff.
- 18:24There's a lot of guarantees around the way it's designed.
- 18:27There's groups for broadcast and then sort of this interface or API for sort of low level talking and communication between processes.
- 18:34And on top of those primitives is built.
- 18:37The WebSocket spec is built the HTTP spec.
- 18:39And if you want to, as we're doing it at Eventbrite, in fact, you can then take that low level spec and build, say, a service oriented architecture system on top of it as well.
- 18:48And that's what we're doing at Eventbrite.
- 18:49Oh, very cool.
- 18:50So you guys are using Django channels at Eventbrite?
- 18:52Well, so in particular, the underlying layer.
- 18:54So the underlying layer is rather unimaginatively called ASGI or ASCII.
- 18:59And what it is, it's definitely not a whiskey replacement.
- 19:02100% not.
- 19:03And what the idea is, it's like ASGI is a specification for there is a thing that can pass messages around on named channels.
- 19:11And you can send to named channels and you can receive from them.
- 19:15And it gives you a few basic guarantees, the methods to call on objects, a bit like dbapi2 is in Python, dbapi2.
- 19:22So things like, you know, there is a send method, there is a receive method.
- 19:27You can do groups in certain ways.
- 19:30And if you just have that, you can take things like the Redis transport we've built that Django channels runs on and use it for whatever you like and do message passing inside just pure Python.
- 19:41Like you don't even need Django.
- 19:42Like that part is a pure Python library.
- 19:45That's really cool.
- 19:45So basically, if you've got something that can act as a transport like Redis, like a Redis Q or something, then you're good?
- 19:51Well, yeah.
- 19:52So it's more than that.
- 19:53So like what happens is like the ASGI spec gives you certain guarantees.
- 19:57For example, it guarantees messages delivered at most once.
- 20:00So either you get it or you don't.
- 20:02The opposite choice being at least once, meaning you get it or you get two of them.
- 20:05And that's a choice you have to make.
- 20:07And it has a lot of those choices like, so you have a message expiry, so you get like 60 seconds.
- 20:12You have back pressure, so you know if a channel is full.
- 20:14Sort of all these sort of guarantees that if you're building any sort of distributed system, you want to know what these guarantees are and sort of program against them so you understand your trade-offs you're making.
- 20:24And so it comes with a preset set of trade-offs and then code built against those ready.
- 20:30So like there's one that uses Redis for transports using lists.
- 20:32There's one that uses a local shared memory segment.
- 20:35There's one that just works inside a process for like sort of unit testing.
- 20:39So you have a nice lightweight one to write unit tests against.
- 20:41And also we have somebody working on a RabbitMQ one right now as well to use RabbitMQ if you want to use that stuff.
- 20:46Oh, yeah.
- 20:46That sounds really cool.
- 20:47A lot of options there.
- 20:49Let me take just a moment and tell you about Metis, a sponsor of this episode.
- 20:53A data scientist's responsibilities can range far and wide.
- 20:57How can you be sure you have the necessary skills and training to enter the field or keep up with emerging technologies?
- 21:03Metis, a data science training company based in New York City, San Francisco, Chicago, and Seattle, provides full-time immersive boot camps,
- 21:11evening part-time professional development courses, online resources, and corporate programs,
- 21:16all with a goal of training you to become a data scientist and help you stay on top of new and necessary skills.
- 21:22Metis has a long list of professional development courses starting soon in any of these four cities taught by industry leaders.
- 21:30Interested in honing your skills?
- 21:31Then these courses are for you.
- 21:33How do they work?
- 21:34Courses on topics like data visualization, deep learning with TensorFlow, machine learning,
- 21:39and statistical foundations run two nights a week for six weeks during the evening hours that fit into your busy schedule.
- 21:45These courses are laser-focused on relevant topics and skills that are sure to enhance your career.
- 21:50They have a special offer for you at thisismetis.com slash talkpython.
- 21:55Be sure to check out what they're offering.
- 21:56It helps support the show.
- 21:58You talked about ASGI, and people are familiar with WSGI, Web Service Gateway Interface.
- 22:05What's ASGI?
- 22:06What's the A for?
- 22:07Asynchronous?
- 22:08Asynchronous, exactly.
- 22:09As I said, it's very unimaginative.
- 22:10Essentially, I had to find a name, and I stupidly chose to go with changing one letter of WSGI.
- 22:17It's not even that similar to WSGI.
- 22:19It does also have a way to transport HTTP over it, and that's kind of where it's semi-related.
- 22:24But also, it's basically more of a specification for message passing at some point as well.
- 22:29So, not the best name, but it's now stuck in everywhere, so I can't really change it.
- 22:34Right, absolutely.
- 22:35Well, and if you look at the WSGI interface, the API, it's really, really simple.
- 22:41And there's really very few hooks for asynchronous programming in there, right?
- 22:45Especially bi-directional stuff.
- 22:46Exactly.
- 22:47In fact, one of the things I love about WSGI so much is how simple it is.
- 22:51The interface, literally, you must be a callable.
- 22:54You must take two arguments.
- 22:56That's the entire interface of WSGI, pretty much.
- 22:58There's a specification of what the environment looks like and where you get your stuff from,
- 23:02but it's very, very simplistic.
- 23:03And so, certainly, that was an inspiration to me of like, well, I want something that's
- 23:08similarly very simplistic that you can write against as well.
- 23:10And so, that kind of came along.
- 23:12But the thing, and the place where WSGI falls down, is that it has no support for asynchronous
- 23:18programming at all.
- 23:19Like I said, it's a callable.
- 23:20It's not a yielding thing.
- 23:22It's not a twisted deferred returner.
- 23:24It's just a blocking callable that returns when the request is done.
- 23:27All right.
- 23:28That's like the exact opposite of asynchronous APIs.
- 23:30Pretty much, yeah.
- 23:32Interesting.
- 23:33So, if I write some code here against ASGI, how interchangeable is that?
- 23:41Like, what web servers kind of run around?
- 23:44Like, a uWSGI, GUnicorn.
- 23:47Like, does it plug into any of those?
- 23:48Or what's the story there?
- 23:49There's two separate things.
- 23:51There's ASGI, which is like sort of the same.
- 23:53Like, here's the thing you can send messages to.
- 23:54And then there's the HTTP over ASGI specification.
- 23:58That's kind of the WSGI equivalent, right?
- 24:00That's like the, here's how we encode requests.
- 24:02Here's where the URL variables come.
- 24:05Here's how you send a chunk response.
- 24:06Like, that stuff is specified as like the format of messages you send over the channels,
- 24:10basically.
- 24:10And because of the way it supports more than WSGI, it is, of course, not directly compatible.
- 24:18And so, in particular, right now, there is one reference server, which is called Daphne.
- 24:23Daphne is basically bits of Twisted and bits of Autobahn, which is the really good Python
- 24:29WebTockets library, sort of hot glued together in a way that just sort of does the specification.
- 24:35And people are also working on, there's been a uWSGI plugin being worked on.
- 24:40Somebody has very bravely been working on a Microsoft IIS plugin, which is really impressive.
- 24:46Oh, yeah.
- 24:46That's interesting.
- 24:47Serious work going into that one.
- 24:49But in particular, there's also the idea that it's a superset.
- 24:54And so, an adapter from one to the other is not hard to write.
- 24:57And in fact, there is a half written adapter already in the code base, where you can plug
- 25:01in an ASGI backend to a WSGI server.
- 25:06It sort of just translates between them.
- 25:07But the key problem is, of course, that doesn't let you run WebSockets because WSGI doesn't
- 25:12support WebSockets.
- 25:13And so, that lets you sort of proxy things to servers that look like old traditional
- 25:20websites, but doesn't give you all the new features that you might want.
- 25:22And for those new features, things will have to be like natively supported, basically.
- 25:26Yeah.
- 25:26So, that's why you've got things like Daphne.
- 25:28Exactly.
- 25:28So, Daphne is like sort of the reference server.
- 25:31And I would love to get like a proper uWSGI support in there as well.
- 25:35Like a second option, maybe Junicle.
- 25:37I just sit down with these servers and like seriously look at them and see if I can like
- 25:42think of like, can I patch them or like, can I change the design set?
- 25:46Like just solve that story.
- 25:48So, I'm not just like here, run my special web server.
- 25:51It'll be fine.
- 25:51It's written by just Andrew on his spare time.
- 25:54It's probably okay.
- 25:55And change that story.
- 25:57Like there is a ecosystem of things you can pick from is the idea.
- 26:01Yeah.
- 26:01That's really fantastic.
- 26:02And cheers to the guy doing IIS because when you're hosting on Windows or you want to host
- 26:07it in somewhere like Azure or something like, that's the only choice, right?
- 26:10It's not like Linux where there's a variety of things you put together.
- 26:13It's just like people on Windows, they just use IIS and that's that.
- 26:17So, that's really cool that it could unlock that.
- 26:19Let's talk a little bit about the sort of Django patterns.
- 26:23Like how is this similar or different to the stuff that people already know?
- 26:28Like the idea is to sort of keep it familiar.
- 26:30So, obviously, we can't make it the same as Django because like some of the things that
- 26:35sort of hobble Whiskey also hobble Django.
- 26:38Like Django views are the same kind of they take a request, they return a response.
- 26:42You can't really do much inside them.
- 26:44And so, my goal was to take things that look and feel familiar and crucially have the same
- 26:49safety guarantees as normal Django.
- 26:50So, you can code with them the same way but also support these extra abilities.
- 26:55And so, like the main thing here is and the sort of top of the thing is things called
- 26:59consumers and consumers map pretty much to Django views.
- 27:02And the idea is whereas a Django view takes a request and returns a response, a channel's
- 27:08consumer gets a message and then can send zero or more other messages.
- 27:12So, for example, if you wanted to listen to incoming WebSocket like chat messages, the
- 27:19consumer will be tied to the request channel.
- 27:21And every time you send a thing in on WebSocket, it would be like, okay, I've got a message.
- 27:25It would launch the consumer.
- 27:26The consumer can run, handle the message, and then maybe send some messages to other clients
- 27:32or sort of broadcast it, store it to a database, and then it exits straight away.
- 27:36And the key thing here is that the consumer, it takes one argument, which is a message object,
- 27:41which is kind of like the request object.
- 27:42You can have session and auth support the same way you can in Django too with decorators.
- 27:46It's like message.channel session or message.user, that kind of stuff.
- 27:50And also, as long as you stick to the thing of not doing blocking options, blocking things
- 27:56in the consumer, it actually makes it almost difficult, almost impossible to deadlock the
- 28:00process because it's not proper async because you can't block or listen to other channels.
- 28:05And because you sort of have to take the message, run through the logic, and then exit pretty
- 28:10much immediately, it means that we can actually churn through messages in the same way that
- 28:15Django might churn through HTTP requests with a very similar kind of set of guarantees and
- 28:21safety concerns.
- 28:21Well, that's really interesting.
- 28:22I hadn't really thought of the views themselves actually having to be so cognizant of this async
- 28:31world, but it totally makes sense.
- 28:32Yeah.
- 28:33And in particular, the nice thing is that views end up being just a subset of consumers
- 28:38because a view takes a request, which is just a message in channels land, and it returns
- 28:44a response, which means sending a message.
- 28:46So it actually means that views end up being a subset of consumers and it all sort of neatly
- 28:50wraps around.
- 28:51Sure.
- 28:52You talked a couple of times about the ability to basically deadlock yourself.
- 28:56Yes.
- 28:58I've seen that before in some of this WebSocket sort of callback style programming where you're
- 29:03waiting and you can't receive the message because you're waiting, but you're not going
- 29:08to get a response.
- 29:08Can you maybe describe some of the ways in which you could like wrap yourself around in that
- 29:13trouble or just sort of give somebody an idea what to look out for?
- 29:17Yeah.
- 29:17So the classic thing is where you have a process that sort of it gets a message and say it then
- 29:22waited for a second message.
- 29:23It's like, okay, I'm going to send a thing and say, I want X and then wait to get X back.
- 29:28The problem is if one process says, I want X, the other process says, okay, I've got X,
- 29:33I want Y to the other process.
- 29:35They're both then blocked in the thing of waiting for their response, but they both can't serve
- 29:40the response to the other process.
- 29:42And that's a deadlock basically.
- 29:43And that's very easy to get into if you don't have either an excellent knowledge of your
- 29:49system architecture and asynchronous programming.
- 29:52Or if you have a framework that sort of builds in or so what channels it isn't, but channels
- 29:57deliberately doesn't give you full async so that it's much harder to toot yourself in
- 30:01the foot.
- 30:02Like we don't let you block on things because that makes it very hard to like, we said,
- 30:07yeah, you can't, you can't be blocked.
- 30:10You can't deadlock because you're waiting on something.
- 30:12If you can't wait on something, right?
- 30:13Exactly.
- 30:14And if you want to, if you want to have that sort of low level control, you can just drop
- 30:18down into sort of normal Python and just write your own async stuff against the ASGI interface.
- 30:23But the channels level, sort of higher level one that we keep sort of safer and easier to
- 30:29stomach.
- 30:29So the idea is like, if you're coming into this as a web developer or maybe using as a
- 30:33brand new developer, the number of scenarios where you can have not only errors, but errors
- 30:39that are impossible to debug is greatly reduced.
- 30:41because deadlocks not only nasty, but knowing they've happened and debugging that that's
- 30:45what's happening is really difficult.
- 30:47It's super difficult.
- 30:48Yeah.
- 30:48And I heard them described as Heisen bugs.
- 30:52Yes.
- 30:53Because, you know, even when you like know that they're there by observing them or interacting,
- 30:58like putting logging or other stuff sometimes can change the timing, which actually changes
- 31:02them.
- 31:03Right.
- 31:03So super hard.
- 31:05One of the, one of the worst things is they often happen only under high load.
- 31:09So like, imagine you have like 10 worker threads.
- 31:11If you just have like, like five requests a second, it will happen perfectly fine.
- 31:15As soon as you hit like over 10 requests a second, say, and you use all 10 of the threads,
- 31:20that could then trigger the deadlock because all the thread, all the work is suddenly consumed
- 31:24up.
- 31:25There's no spare workers to handle these extra requests.
- 31:27And so like, it might be fine in development and you need to point to production.
- 31:30It's fine in production.
- 31:31And then one Sunday evening when your website's under high loads, suddenly it locks up and there's
- 31:36no traceback.
- 31:37Like what's happening?
- 31:37I don't understand.
- 31:38It's like, it's a really nasty sign of error.
- 31:40Yeah.
- 31:41It's super bad.
- 31:42And I think it's awesome that your framework like makes that harder.
- 31:44That usually that's the kind of stuff that happens right when you need the website to
- 31:50work most.
- 31:50We're running a new ad or we got featured on Hacker News or whatever.
- 31:53Boom.
- 31:54It's dead.
- 31:55Yeah.
- 31:56And it's, it's not perfect, obviously.
- 31:57Like we can't stop everything, but like the Jenga philosophy is try and make these things
- 32:01hard, but a fault.
- 32:02And so that's kind of tried to be reflected here.
- 32:04Yeah.
- 32:04That's absolutely a good philosophy.
- 32:06So some other design patterns around Jenga that are maybe related like routing and messaging.
- 32:11Yeah.
- 32:12So in the other things that's familiar.
- 32:14So routing.
- 32:15So the idea is when you say, I have a consumer of this channel, you have to sort of say like
- 32:20a view.
- 32:20You have to tie that consumer to the channel.
- 32:23So like, well, this consumer handles incoming messages on the WebSocket.receive channel.
- 32:29And it handles the ones where the path is slash chat.
- 32:34And so there's a thing that looks very like Jenga's URL routing, but is channel routing instead.
- 32:40It's like, it's not quite the same, but very familiar.
- 32:43It's like a list of regular expressions basically.
- 32:45And it has, again, very similar guarantees and a lot of power in there.
- 32:49And then, as I said before, messages are very much like requests and that like they have
- 32:53information on them.
- 32:54You can store stuff on them.
- 32:56There's session support in there.
- 32:57It's like, it all feels a bit like you're writing Jenga views and URLs, but it's a bit more powerful
- 33:03with a bit extra, with a few extra options.
- 33:05Okay.
- 33:05Yeah.
- 33:06Very cool.
- 33:06And what's the programming model look like?
- 33:09Like how is it the same as Jenga or how is it different?
- 33:12So to the end developer, it looks largely the same as normal Jenga.
- 33:16In particular, channels in its default state runs all of your code, the writer's consumers
- 33:22and stuff, synchronously.
- 33:23Like you don't ever run into an async option.
- 33:27And particularly we run the whole of Jenga synchronously too, because like we can't make
- 33:30Jenga async.
- 33:31So we just run Jenga synchronously with the sort of, the way design helps us run it in sort
- 33:35of a worker thread.
- 33:36And all the async is handled away from Jenga, basically.
- 33:40So if you're coming in from that end, it looks pretty much like normal Jenga.
- 33:42However, if you want to do sort of more advanced stuff or handle different protocols or even
- 33:47do your own async programming, it sort of drops down and becomes more of a pure Python thing
- 33:53where you can use whatever framework you like that you can use async.io, you can use twisted,
- 33:57you can use other things.
- 33:59And then you just call the ASGI API, send and receive stuff as you need to.
- 34:05So for example, Daphne is a purely asynchronous twisted program that happens to call ASGI in
- 34:11the right way inside of it.
- 34:12So like you sort of get the choice of, do you want the familiar happy world of Jenga without
- 34:16much power?
- 34:17Or do you want the raw unbridled power of async?
- 34:20But you sort of need to be prepared to go into that particular world.
- 34:24Yeah, you're going down the rabbit hole there.
- 34:26Interesting.
- 34:26Exactly.
- 34:27So is this, if I'm doing the async variant, is this still in the same process as Jenga
- 34:32or is this like a totally different thing?
- 34:35So it's a different process.
- 34:36In particular, like one of the things when I sat down, part of that initial period when
- 34:40I was sort of like trying to design channels and go through the different options, I had
- 34:43some things that were threading, some things were using the multi-process module, some things
- 34:48were using green threads or like yielding like generators.
- 34:52And eventually I realized that if you're building a large system, as you know, as I do at work
- 34:59on a daily basis, you're always going to have more than one server, which means you're always
- 35:03going to have to have more than one process.
- 35:04And so given that, if I made a model that was just natively multi-processed from the get-go,
- 35:10that would make a lot of sense in like, not just multi-process, multi-server.
- 35:13Like let's make the model that is by default, it expects to run everything on like a different
- 35:17CPU entirely with no shared memory or no shared state and work backwards from there.
- 35:22And so everything is basically done in a separate process and message passing happens
- 35:27between them.
- 35:27And so that's how things are coordinated.
- 35:29So it's a bit like, say like if you've ever used Go, for example, Go, a lot of Go primitives
- 35:35rely heavily on passing things between Go threads in a thing they call channels as well, which
- 35:40is similar but different to channels.
- 35:41Yeah, I was going to ask you that about the relationship.
- 35:44The name is the same.
- 35:45The idea is kind of similar in some ways.
- 35:47Like, was there any inspiration or similarity?
- 35:51Is it just random similarity or is there more to it with Go and their channels?
- 35:56Oh, no.
- 35:56So the direct similarity that they both share a common ancestor, which is the language called
- 36:00CSP.
- 36:01So when I went to university in Oxford, they invented CSPs.
- 36:05They definitely teach it and they're very proud of it.
- 36:06It's a language for basically proving asynchronous programs.
- 36:10And so it's sort of a very abstract grammar of like, oh, we have a process that emits event
- 36:15X onto channel Y and then a process that consumes channel Y and then gets event X and then does
- 36:20Y.
- 36:20And in its core, it's kind of this way of sort of like you can write a set of assumptions
- 36:25and prove that programs don't deadlock, which is an incredibly nice way of doing things.
- 36:28But it also has these fundamental ideas of processes and channels and splits.
- 36:32And so that's where my ideas came from.
- 36:35And then sort of via Go, I think the name channels came from Go as well, but like a different take
- 36:40on that same core concept of these are communicating sequential processes, which is what CSP stands
- 36:46for.
- 36:47Yeah, this whole actor model is quite powerful for avoiding deadlocks and race conditions
- 36:53and, you know, writing to the same piece of memory at the same time.
- 36:56And a lot of the threading problems go away, right?
- 36:58And not just that, but also like understanding as a developer, like reasoning about systems
- 37:02is much easier when they are very separate components you can read about individually.
- 37:06Like if it's all integrated into one big lump, then trying to work out what happening is pretty
- 37:11difficult.
- 37:11If they're separated out, it's like, oh, here is Daphne.
- 37:14Daphne just translates HTTP requests to messages and messages to HTTP responses.
- 37:19Writing tests for that is a lot easier because we just write tests where we put one thing
- 37:24in one end, the other thing comes up the other end and vice versa.
- 37:26Yeah, you have very clear, explicit boundaries.
- 37:28These are the messages.
- 37:29Here's where the messages come in.
- 37:31Here's where they go out.
- 37:31And basically, this is what they consist of, right?
- 37:34Yeah.
- 37:34And then that scales up too.
- 37:35Like if you're doing engineering at scale, as in with a large team, those boundaries then
- 37:39become team boundaries.
- 37:40That really helps you sort of distribute not only the testing, but also like the cognitive
- 37:46workload of understanding what the system is doing at any one time that you can draw the
- 37:49same boundaries and have teams work on shared understanding in a much better way as well.
- 37:53Yeah.
- 37:54That's a really good point to think about how it lets people scale, not just software.
- 37:57Oh, yeah.
- 37:58Scaling.
- 37:59I'm very proud these days that scaling software development is about half code scaling, about
- 38:04half people scaling.
- 38:06Like it's very important to get your team all understanding each other and not being at each
- 38:10other's throats all the time.
- 38:11And a big part of that is good specs and good APIs.
- 38:13Yeah, that's awesome.
- 38:25This portion of Talk Python To Me is brought to you by Hired.
- 38:28Hired is the platform for top Python developer jobs.
- 38:31Create your profile and instantly get access to 3,500 companies who will work to compete
- 38:35with you.
- 38:36Take it from one of Hired's users who recently got a job and said, I had my first offer on
- 38:40Thursday after going live on Monday and I ended up getting eight offers in total.
- 38:44I've worked with recruiters in the past, but they've always been pretty hit and miss.
- 38:47I tried LinkedIn, but I found Hired to be the best.
- 38:50I really liked knowing the salary up front.
- 38:52Privacy was also a huge seller for me.
- 38:55Sounds awesome, doesn't it?
- 38:56Well, wait until you hear about the sign-in bonus.
- 38:58Everyone who accepts a job from Hired gets a thousand dollars signing bonus.
- 39:02And as Talk Python listeners, it gets way sweeter.
- 39:04Use the link Hired.com slash Talk Python To Me and Hired will double the signing bonus to
- 39:09$2,000.
- 39:10Opportunity's knocking.
- 39:11Visit Hired.com slash Talk Python To Me and answer the door.
- 39:22So if I'm going to run Django app using channels, how's this work?
- 39:27Can I run it the same?
- 39:28Probably Daphne is involved somewhere, right?
- 39:31Basic level, you run two processes.
- 39:33You run Daphne.
- 39:34And what Daphne does is Daphne terminates HTTP and WebSockets for you.
- 39:38So that's what you expose to the world on port 80 or whatever, or probably behind Nginx doing
- 39:43static file serving or something.
- 39:45And that sort of fills the role that, say, like Gunicorn might fill.
- 39:47Like, here's a process that terminates HTTP.
- 39:49And then separately, you run one or more Django worker processes.
- 39:54And those processes look at the channel queues that are in this sort of channel routing we
- 39:59mentioned earlier and sort of just run the consumers on those channels as messages come
- 40:04through.
- 40:04And so at a base level, you run one Daphne and you run one worker.
- 40:08And then it just sort of works as normal Django does.
- 40:11And in particular, I see.
- 40:12So the request comes in and if it's like a channel request for, say, WebSockets, it just
- 40:18stops at Daphne and gets processed there.
- 40:21But if it's something that matches for Django view or something, it will like pass through
- 40:26to the Django worker process?
- 40:28No.
- 40:28So in fact, Daphne does both of them.
- 40:30So like both HTTP and WebSockets both get turned into messages on channels.
- 40:35So that's kind of the difference to a lot of those previous solutions I talked about near
- 40:39the start of the show is that channels has a sort of slightly ambitious goal maybe of putting
- 40:45everything over channels.
- 40:46This includes HTTP as well.
- 40:48So what happens is if you have a HTTP request that comes into Daphne, Daphne takes it, it decodes
- 40:53it into the ASGI HTTP format, pass that to Django.
- 40:58And then Django has a native ASGI handler next to the WHGI handler that takes that, turns it
- 41:04directly into a request object and then runs the views on that stuff too.
- 41:06Okay.
- 41:07Yeah, that's very cool.
- 41:08So it is the same system as the channels running over basically.
- 41:11Okay.
- 41:12Excellent.
- 41:12And you still have things like run server and so on?
- 41:16Right.
- 41:16So run server, like as for development, just you type run, like if you install channels
- 41:20or type run server, it just runs Daphne and worker for you in the same process.
- 41:24It's no sort of fitting around there.
- 41:25It's just easy to get going.
- 41:27And it means you basically, you can pip install channels, put it in your installed apps, and
- 41:31then WebSockets just work on your development server straight away, which is really nice
- 41:34to have for sort of hacking around.
- 41:36And then sort of the other thing you can have, and I sort of recommend, especially to people
- 41:40who are wary or like people who are like died in the wall ops people, should we say, is
- 41:45you can run both WSGI and channels at the same time and just route WebSockets to Daphne
- 41:51and then route normal requests to say MicroWSGI or GUnicorn.
- 41:55And so that way you can use sort of more proven software for your main site, but still use
- 42:00channels for your other stuff if you want that sort of that trade-off.
- 42:03Oh, that's pretty interesting.
- 42:04And you're thinking put something like Nginx in front of it and switch on like protocol
- 42:08or something.
- 42:09Yeah.
- 42:09So you can switch these things, switch on as URL paths, like things on us like slash WS
- 42:14WebSockets, but you can, with a couple of tricks, switch on the upgrade header as well,
- 42:18which is how you upgrade WebSockets.
- 42:20So like there are ways and means.
- 42:21It's tricky to switch dynamically based on what's coming in.
- 42:26Like you can do it.
- 42:27It's very hard to.
- 42:28So generally I recommend like sort of a path-based solution.
- 42:31And like there's examples in the channels docs and mailing lists about like how that might
- 42:35run as well and sort of examples how to set that stuff up.
- 42:37All right.
- 42:38So what's the extensibility story here?
- 42:39So channels is in fact, it's very much built around extensibility.
- 42:42Like as I said before, like it's not that much in the base package.
- 42:46You sort of WebSocket supports there and that kind of stuff.
- 42:48But like it's designed to be built upon in sort of a community way like Django's itself.
- 42:53So things, for example, like the ability to have proper heart beating of people connected.
- 42:59So, you know, they're definitely still connected.
- 43:00Like that is a thing that you could put on top of channels.
- 43:03And so what I'm trying to do is like foster a community of third-party apps that are built
- 43:07against channels and the common specifications.
- 43:09In many ways, that's why I took it upon myself to build a sort of more official, it's not the
- 43:16right, but to build a singular solution that we can sort of almost tout as Django solution
- 43:19for this stuff.
- 43:20So that there is a common thing that people can build against.
- 43:23Because like a lot of the part of Django success is having a common standard people write against.
- 43:29Like it's a common known factor.
- 43:30And so really it's designed around that idea of like, yeah, everyone's going to have the
- 43:34same routing.
- 43:35Everyone's going to have the same idea of consumers.
- 43:37Like things integrate the same kind of way and like trying to encourage that kind of ecosystem
- 43:42as it were.
- 43:43Yeah, absolutely.
- 43:44I, you know, that's definitely a hallmark of Django is all the pieces you can bring
- 43:48together to make it more than what it is out of the box.
- 43:51Exactly.
- 43:51Nice.
- 43:52Are there like some notable third-party packages or projects that you want to talk about?
- 43:57Not, not, I can talk to my head.
- 43:58Like I don't know them off by heart.
- 44:00We have, we have a page on the docs that links to a couple of ones that are already going.
- 44:03Okay.
- 44:04Awesome.
- 44:04There's also a few more that aren't on there yet.
- 44:07I encourage the developers to add links to the doc.
- 44:10Okay.
- 44:11So let's see.
- 44:11We talked about Daphne a little bit.
- 44:13Tell me some of the packages that you're using to put this together.
- 44:16Yeah.
- 44:17So like one of the things was like trying like Django for a long time is criticized as being,
- 44:21oh, Django is one giant package.
- 44:23It's not split in separate bits.
- 44:24So I went the entire opposite way.
- 44:26There are five different packages as part of channels.
- 44:28So you have the base channels package, which is the, basically the Django plugin side of
- 44:34things as Django third-party app or second-party app, I guess, or first-party app.
- 44:38But it's a Django pluggable app that you load into Django.
- 44:41It does all the routing and run server overloading we talked about before and gives you sort of
- 44:44the nice user experience.
- 44:46But it doesn't give you the serving.
- 44:47And so Daphne is the other half of that.
- 44:49Daphne is the service.
- 44:51So Daphne is the server that terminates HTTP and WebSockets and sort of translates them through
- 44:57into channels for you.
- 44:57If there was something else there, you could just use channels and a different thing that
- 45:01wasn't Daphne.
- 45:02But for now, we bundle them together pretty much.
- 45:04And then underneath that, there is the ASGI packages.
- 45:07So the things that build this sort of low-level protocol we build upon.
- 45:10So there's ASGI ref, which is sort of the base ASGI library.
- 45:15It has the conformance test suite in it.
- 45:16So you can, like, if you're writing a new ASGI backend, you just load up the conformance
- 45:20tests and it just tests your spec, which is really nice to have for new implementations.
- 45:24And it has a memory backend too for testing, like, in-memory for unit tests.
- 45:27And on top of that, you have two different backends.
- 45:30The Redis backend called ASGI underscore Redis.
- 45:32And that is a standard Redis backend, the one we recommend for production use.
- 45:36It uses Redis lists and blocking pops and Lua scripts and a few other things in there.
- 45:42And it has built-in sharding if you want to use more than one server to scale up.
- 45:45And then as sort of a midway point, there's a ASGI underscore IPC backend that only works
- 45:51on one server.
- 45:52And it's designed for, if you don't want to run a separate Redis server as well, you
- 45:56just want to run things on one machine, keep it simple.
- 45:58It doesn't perform nearly as well, but it is zero configuration.
- 46:02You just basically just load it up as the option backend, just set the same prefix and things
- 46:07just talk to each other on the same machine via a shared memory segment.
- 46:10Oh yeah, that's really nice.
- 46:11Really nice.
- 46:11So what's the story with Python 2 versus Python 3 on this project?
- 46:16So everything is built Python 3 first.
- 46:19And in fact, Django itself is going Python 3 only very soon.
- 46:22as of the next major release, I believe.
- 46:24That's in Django 2.
- 46:26And so it's built with Python 3 first, but also Python 2 support is still there.
- 46:31So channel supports the previous LTS of Django and up.
- 46:35So that's 1.8 and up.
- 46:36And 1.8 supports Python 2.7.
- 46:38So it supports Python 2.7 and Python 3.
- 46:41All my testing and all my developments done on Python 3.
- 46:43And then we have Travis just running Python 2 tests occasionally.
- 46:47And I'll just occasionally pop down to Python 2 and make sure things work expected.
- 46:50But I've seen like one bug that was Python 2 specific in the entire time I've been developing
- 46:56so far.
- 46:56So it's not really a concern at all in that regard.
- 47:00Oh, that's really fantastic.
- 47:01And I'd love to see you doing this in Python 3 first.
- 47:04You know, I talked to the guys at the Beware project, and they're doing Python 3 only.
- 47:09I'm surprised to hear about Django going Python 3, although excited to hear it.
- 47:14I think Python 3 is really, it's crossed some kind of threshold or critical mass recently,
- 47:20I feel.
- 47:21Yeah, like, I feel the same way too.
- 47:22Like, in the last year or year and a half or so, we definitely crossed that threshold of,
- 47:26like, things are coming out in Python 3 only, a lot of cases.
- 47:30We're finding libraries at work now that were like, oh, this great accounting library is only
- 47:35on Python 3.
- 47:36And then suddenly, like, we need to move code to Python 3.
- 47:38It's become almost this trend now.
- 47:40And, like, it's difficult for a big company like Eventbrite to move to Python 3, of course.
- 47:44But definitely, a sea change is definitely happening these days.
- 47:48And everything I write from scratch is done on Python 3.
- 47:51Yeah, that's fantastic.
- 47:52I feel like I'm noticing it in that people are apologizing for it being Python 2 in the
- 47:59cases where it is, or they feel guilty or bad about it or whatever.
- 48:03Whereas a few years ago, it was kind of like, yeah, it's Python 2, of course.
- 48:06Like, haha, Python 3, whatever, right?
- 48:08So this is really cool.
- 48:09Tell me a little bit about Django going Python 3, if you know any of the backstory.
- 48:14What's going on there?
- 48:14So we decided quite a while ago to draw a line in the sand, as it were.
- 48:18So, like, you know, at some point, we need to do Python 3 only.
- 48:21In many ways, because Django's job is, in some ways, as a leader in this area.
- 48:27Like, if Django is Python 3 only, that's a big signal to a lot of other Python packages that,
- 48:30yes, you can do this too.
- 48:32It's a huge signal.
- 48:32It's a huge signal.
- 48:33And so the line was decided.
- 48:35And, like, we debated for a long while.
- 48:37I'd be like, well, like, what version of Django should we do it on?
- 48:41Like, when should we do it?
- 48:42And so different core developers were arguing at different times for, like, oh, well, can
- 48:47we do it now?
- 48:48So I think, like, two or three years ago, people would say, like, oh, let's just do it now.
- 48:51Let's just switch straight away.
- 48:52And then eventually we decided that Django 2.0 is the first release to only support Python
- 48:593.
- 48:59And in particular, Python 3.5 and above.
- 49:01Because the reasoning being that Python 3 is small enough that people are pretty much
- 49:05on the newest version of Python 3.
- 49:06There's not many differences that start upgrading to Python 3.5.
- 49:09And so what this means to Django in particular is we can remove swathes of compatibility code.
- 49:15Like, there's so much in there.
- 49:16Some of the core devs are literally chomping at the bit to just delete huge reams of code
- 49:21that are just Python 2.0 specific.
- 49:23And in particular, like, we can use things like all the new features.
- 49:26We can get some of the class inheritance stuff and get done properly.
- 49:28Like, all the sort of stuff that we had to sort of keep going with Python 2 and keep it
- 49:32there is all being swept away.
- 49:34And the nice thing, too, is that because it's Django 2.0, we can say, okay, this is a nice
- 49:39big change.
- 49:40And in fact, like, the Django number system is changing entirely.
- 49:43So for reference, what's happening is we're making it so that Django's releases are...
- 49:48There's every major LTS will be the last number in its major number.
- 49:53So like 1.11 will be the last one.
- 49:55Then we'll have 2.0, 2.1, 2.2.
- 49:57Then 2.3 is the next LTS.
- 49:59And 3.0, 3.1, 3.2.
- 50:00And 3.3, the LTS.
- 50:01And that kind of strategy.
- 50:03And it just happens that it's kind of timed nicely that Python 3.5 support happens in
- 50:08Django 2.
- 50:09So it's a good reason to call it Django 2, I think.
- 50:12We were worried for a while in Django Core that there wouldn't be a good thing in Django
- 50:152.
- 50:15We'd be like, oh, and this is just another maintenance release of Django with some bug
- 50:18fixes.
- 50:19But it's nice to have a bit of a sort of big change in there as well.
- 50:21Yeah, that's really cool.
- 50:23And I think changing the major version number sends a strong signal like this thing is different.
- 50:28Exactly.
- 50:28And it's easy to pull.
- 50:30I've been doing for the last three or so years now, pausing code to Python 3, really not that
- 50:37hard.
- 50:37Once you get your head around what's going on, and if your encodings are already good.
- 50:42It's like Python 3, and I like this about Python 3, really punishes you for not having good
- 50:47encodings, like knowing where your bytes come from.
- 50:49Which for developing channels, which is all about handling bytes over the wire versus encoded
- 50:54messages or URLs, is incredibly useful.
- 50:57Like, oh, well, you've tried to pass this byte string to the Unicode string function.
- 51:01It just blows up.
- 51:01It's like having, as long as that's all in order and you know where your strings are coming
- 51:05from, it's pretty painless to port in my experience.
- 51:08Yeah, that's really excellent.
- 51:09And it is sort of that network layer that you see some of the differences between Python
- 51:152 and Python 3 more when you're down at the string level and taking bytes and converting
- 51:19them.
- 51:20But yeah, it sounds really good.
- 51:21Yeah, it's really good.
- 51:23Like, my main complaint is that I keep typing print with a space rather than print with a
- 51:26parenthesis.
- 51:26Like, muscle memory is a really hard thing to undo.
- 51:29Yeah, absolutely.
- 51:30So when we talked about WSGI and we talked about AWSGI, the asynchronous thing, what came
- 51:40to mind for me was this concept of WSGI 2, which is being sort of brought into existence by
- 51:48the pressure that HTTP 2 is putting on the WSGI specification, right?
- 51:52Yeah.
- 51:53Other pressures too.
- 51:54WebSockets is part of those pressures as well.
- 51:55Yeah, absolutely.
- 51:56Well, I was, you're right.
- 51:58WebSockets predates HTTP 2.
- 51:59But all this sort of, we want to run more than one thing over this channel and we want
- 52:03to sometimes do it in binary, like just doesn't work at all.
- 52:07So can you talk a little bit about like WSGI 2 and maybe how that like would either help
- 52:13or is parallel to what you're doing?
- 52:16Yeah.
- 52:16So like I actually involved in discussions for a while.
- 52:18So there's a group in Python called WebSig and WebSig is sort of the place where things
- 52:23about WSGI get discussed.
- 52:24And there's been on and off for like a few years now, at least, I came in pretty late to
- 52:29discussion, this idea of WSGI 2.
- 52:32And one of the main problems is that if you want to call it WSGI, it has to look a lot
- 52:38like WSGI and in theory, backwards compatible as well.
- 52:42And that really hinders the approach.
- 52:44And like, as of the time of recording, there's been very little progress on WSGI 2 for like
- 52:49about two years, if not longer.
- 52:51Lots of different ideas about what should be done or different approaches, like how do we
- 52:55even make things async?
- 52:56And so my conclusion to that was that like Django needs something, we needed something
- 53:04that existed and worked.
- 53:06And I read through all the threads and tried to educate myself on the arguments and the
- 53:10reasons behind different people's visions and wants for WSGI 2 and took some of those
- 53:15with me to the design of ASGI and in particular the HTTP layer on top of it.
- 53:20But at the same time, I don't want to stride in and declare a replacement for the WSGI
- 53:26with ASGI, rather it's a different thing.
- 53:28I also think that a different thing is the right solution.
- 53:31I think that Python web systems are more than just handling HTTP responses these days.
- 53:39And so my ultimate goal is probably to try and sit down, get Django and get more implementations
- 53:46of this going through.
- 53:47And then when we have a good set of reference implementation, like two references on either
- 53:51end, it's proven in production, then take that proven thing back to WebSIG and say, hey,
- 53:58can we decide on this thing that's already in place that has proof behind it?
- 54:02And then we can see what's happening with it rather than trying to argue in the abstract,
- 54:05basically.
- 54:06Yeah, I think arguing in the abstract is really hard to do.
- 54:10It's very difficult.
- 54:11Yeah, you can just go around and around and around.
- 54:14And I, you know, I don't know exactly what's happening internally at WebSIG, but if it's
- 54:19been kind of idle for two years and HTTP2 is coming, the web is not standing still.
- 54:23So I feel like, you know, maybe there's some of that going on.
- 54:27I am perfectly willing.
- 54:28If someone comes up tomorrow with an amazing WSGI 2 spec that everyone adopts, I will immediately
- 54:34drop and support that stuff.
- 54:35But like, given the lack of movement, I felt that someone has to do something.
- 54:40And so I decided to be that someone and do something.
- 54:42Yeah, this is really cool.
- 54:43I mean, if you get this to be successful and proven, like you said, it's really easy to
- 54:48take it back and go, look, this is more or less working.
- 54:52Let's make a spec.
- 54:53Let's extract a spec out of this general idea rather than debate at an infinite item.
- 54:58Exactly.
- 54:59I've been trying to keep conversations going with like various authors or luminaries of
- 55:03Python web stuff.
- 55:04Like not enough, but like getting opinions and feedback on like the way things are done.
- 55:07It's like silly things like cleaning up the, like I said, encoding, cleaning up the encoding
- 55:12of how strings are passed around in ASGI.
- 55:14Like ASGI has an exact specification of which bits of the request come through as Unicode
- 55:20and which bits come through as bytes, which WSGI does not have and can kind of never have.
- 55:24So like I've been trying to keep a lot of those things in mind.
- 55:27And I don't know if it'd be successful in the end, but I'm hoping that like pragmatism is worth
- 55:33a lot here.
- 55:33And even if the message passing sort of asynchronous bit doesn't survive, the basic format of here's
- 55:41how you take a request and put it into a message dict would still be useful.
- 55:44Yeah.
- 55:45I think it's really positive that you're working on this and hopefully, hopefully that can,
- 55:50you know, fall this, this blockade or whatever, whatever is going on there.
- 55:55Some point like doing open source collaboration is really difficult.
- 55:58Like it takes a lot of effort.
- 55:59So like it takes not just me, but other people.
- 56:03So hopefully we can get something, something out.
- 56:04Yeah.
- 56:05Hopefully.
- 56:05I mean, WSGI itself has been so important and so successful that it's just really scary
- 56:11and hard to change.
- 56:12So I think that's, it's almost a victim of its own success in that regard.
- 56:16Yeah.
- 56:16WSGI is like right next to CGI and like how pervasive and successful it's been.
- 56:22Like every like Python web program runs on it.
- 56:26It's incredibly, it's incredibly simple.
- 56:28It's very powerful and the pluggability of different servers is like a huge boon in its
- 56:33deployment, right?
- 56:34Like deployment in Python is never as easy as PHP, but it's pretty damn close considering
- 56:39like we have all these different options.
- 56:40So that's really something to aspire to.
- 56:43I think like WSGI is an amazing specification that I am always in awe of, like every time.
- 56:49Like I'm very pleased that the original authors like decided to settle on it like so long ago
- 56:55as well.
- 56:56Yeah, absolutely.
- 56:56It's been going like 15 years or something and it's still going strong.
- 57:00It just, it's starting to show its age.
- 57:03That world was much less about serving multiple things and bi-directional stuff over the same
- 57:09channel and all sorts of things like that.
- 57:10Right.
- 57:11To survive 15 years in software is an incredibly long time.
- 57:14Like when Django hit 10, I was feeling, I was already feeling like a bit old.
- 57:18Right.
- 57:19So yeah, like it's, it's really impressive.
- 57:21Yeah, absolutely.
- 57:22Okay.
- 57:23So that's, that's really great.
- 57:24So maybe, maybe one final question is how's all this useful beyond Django?
- 57:30Like it's, it's cool that we can plug it into Django.
- 57:32What else can we do with it?
- 57:33So like really my goal, my ambitious goal at the end of all of this, I think is to make
- 57:39writing distributed systems in Python easier.
- 57:42And maybe not even in Python, maybe in general, but like, you know, I am, as like my career
- 57:47goes on, I find myself writing software, distributed systems are the one challenge I keep both enjoying
- 57:53and the challenges me time and time again.
- 57:54And like, I would love to have this idea of message passing, this idea of channels and passing
- 58:00messages and these, these primitives be the way that Python programs in general, like not
- 58:05just HTTP, but like chatbots and like internet of things systems and like stats systems, all
- 58:12these things have this common base construct to build upon and talk to each other and message
- 58:16pass.
- 58:16And like, I think that's the ultimate goal beyond Django.
- 58:19There's certainly a long way to get there.
- 58:21The middle goal is to have other Python web stuff work with it as well.
- 58:25But I have like, you know, and I think it's nice to have somewhat grand ambitions for improving
- 58:31Python in general.
- 58:33Like the, like, I like Python, the language a lot.
- 58:36Like I'm not just a Django person, right?
- 58:38Like Python is my real home in many ways.
- 58:40So I want to make sure that everyone can benefit from this stuff and to really push the state
- 58:44of the art forward in that respect.
- 58:45Yeah.
- 58:45That's, that's really cool that it's, it's got that sort of broader goal.
- 58:49You know, you're talking a lot about moving to Python three and this is Python three first.
- 58:54Do you see a world where you could use this idea of channels and this as the infrastructure
- 58:59for those channels mixed in with like async and await to be, to make Python much more go like?
- 59:06That's kind of one of the ideas.
- 59:08Yeah.
- 59:08It's like, it's like channels in the spec, ASGI in the spec rather, has provisions for a
- 59:13async code compatible receive command.
- 59:15So you can do, you can await on receive and how it blocked correctly and do all that kind
- 59:19of stuff.
- 59:19So the idea is really that you can, and moreover, like it's not just that you can write async code,
- 59:25it's that you can also write synchronous code and have that intermixed.
- 59:29Like, cause I'm a strong proponent of if the code you can write can be synchronous or
- 59:34at least can be written in a synchronous fashion, that's fine.
- 59:37As long as it interoperates properly, like synchronous code is easier to reason about.
- 59:40It's easier to test in many cases.
- 59:42So if we can have a system that supports both async code natively and synchronous code natively
- 59:48and they interact properly and you can plug and replace different parts of the system correctly
- 59:52and like, oh, well, we've written this part of the system, like, you know, this waiting
- 59:56room system here is in ASUNKO, whereas like this emailing system here is in twisted and
- 1:00:03this part of the system over here is in synchronous code.
- 1:00:05I think that's also a good goal to aim for, like flexibility is really important.
- 1:00:08Yeah, absolutely.
- 1:00:09All right.
- 1:00:10That sounds, that sounds like a cool feature.
- 1:00:12I'm looking forward to seeing it.
- 1:00:13Let's hope we get there.
- 1:00:14For sure.
- 1:00:15All right.
- 1:00:16So I think maybe we'll just leave it here.
- 1:00:18This is a really cool project that you put together and I'm excited to see it gain
- 1:00:22attraction.
- 1:00:22Thank you.
- 1:00:23Yeah, you bet.
- 1:00:24So before I let you out of here, let me ask you two questions I always ask at the end of
- 1:00:28the show.
- 1:00:29First of all, if you can write some code, Python code, other code, what editor do you open
- 1:00:33up?
- 1:00:33I'm a Sublime Text user.
- 1:00:34I have been for like four or five years now.
- 1:00:36It's just, it's got Python in it too.
- 1:00:38So you can't argue with that really, can you?
- 1:00:39And now it's definitely the home for Python there.
- 1:00:42Have you checked out the Anaconda plugin for it?
- 1:00:45I haven't, no.
- 1:00:46Okay.
- 1:00:46I have my own sort of set of plugins and some custom plugins I've collected over the years
- 1:00:51that sort of, they sort of work together mostly.
- 1:00:53It's pretty decent.
- 1:00:54Nice.
- 1:00:55All right.
- 1:00:55And there's 96,000 packages on PyPI.
- 1:00:59There's a ton that you've probably come across that people haven't heard of necessarily or you'd
- 1:01:04like to recommend.
- 1:01:04What's on your mind?
- 1:01:05And the one I've used most recently, I was really impressed with it is Cryptography, which
- 1:01:09is actually quite new given the name is quite generic.
- 1:01:11It's a really well done crypto package that is designed in a way where it's very hard
- 1:01:16to shoot yourself in the foot.
- 1:01:17As you know, I'm a big believer in this.
- 1:01:19It comes with these amazing, fully featured primitives for, oh, here is a way to do symmetrical
- 1:01:25mesh encryption.
- 1:01:25Here is a way to do asymmetrical public private key encryption.
- 1:01:28And it's just like, there's safe defaults.
- 1:01:30It's easy to use.
- 1:01:31And as long as you follow the docs, you end up with a really nice solution that is compatible
- 1:01:37properly.
- 1:01:37And like, it's pretty quick.
- 1:01:38And like, that's what we use inside ASCII reddit if you want at rest encryption.
- 1:01:42So like cryptography is my current favorite PyPI package.
- 1:01:45Oh, that's really cool.
- 1:01:46I definitely think cryptography and hashing and password management, that stuff is better
- 1:01:52put into a library or package and just really baked in with the best practices and you just
- 1:01:57follow the steps.
- 1:01:58Yeah.
- 1:01:58I do not want to have to think about it.
- 1:01:59Like I want somebody else who's more intelligent than me to solve the problem so I can use their
- 1:02:03code.
- 1:02:04It does that really well.
- 1:02:05Yeah, that's great.
- 1:02:05I'm going to have to check that out.
- 1:02:06All right.
- 1:02:07So before we get out of here, final call to action, how do people get started with channels?
- 1:02:11Like what kind of support or help are you looking for?
- 1:02:15So yeah, so like I'd encourage anyone who's interested to go over to channels.readthedocs.io.
- 1:02:19It's sort of the main channels documentation.
- 1:02:21You can see installation instructions, a short tutorial stuff there.
- 1:02:25And there's also a big list of stuff we want to work on.
- 1:02:27So our GitHub repository, which is github.com/Django slash channels.
- 1:02:31There's a whole list of issues there filtered by experience level.
- 1:02:34You want to have that kind of stuff.
- 1:02:35You can email Django developers or Django users with questions about channels.
- 1:02:39It's all part of the Django project.
- 1:02:40And then if you want to do work on a big project, we even have some funding available for bigger
- 1:02:44projects.
- 1:02:45We can dish out for sort of things that are, you know, at least a couple of weeks, sort
- 1:02:49of bigger tasks that wouldn't be tackled otherwise in spare time.
- 1:02:52So if you're interested in any of those things, like get in touch, mainly this or get in touch
- 1:02:57with me directly.
- 1:02:57I'm Andrew Goldbin on Twitter.
- 1:02:59Just all that stuff.
- 1:03:00I'm happy to talk about channels.
- 1:03:01Oh, that's really great.
- 1:03:03So lots of ways people can get involved.
- 1:03:05So many.
- 1:03:05Yeah.
- 1:03:06Cool.
- 1:03:07Andrew, thanks so much for taking the time to talk with me and sharing your project.
- 1:03:10Thank you so much for having me on.
- 1:03:11It's been a pleasure.
- 1:03:12Yep.
- 1:03:12Bye.
- 1:03:14This has been another episode of Talk Python To Me.
- 1:03:17Today's guest has been Andrew Godin, and this episode has been sponsored by Metis and Hired.
- 1:03:22Thank them both for supporting the show.
- 1:03:24Want to learn data science?
- 1:03:26Well, don't forget to visit thisismetis.com slash talkpython to learn more about their upcoming
- 1:03:32courses.
- 1:03:33Get the skills that you need to succeed in the fast-paced world of data science.
- 1:03:37Hired wants to help you find your next big thing.
- 1:03:40Visit Hired.com slash Talk Python To Me to get five or more offers with salary and equity
- 1:03:45presented right up front and a special listener signing bonus of $2,000.
- 1:03:49Are you or a colleague trying to learn Python?
- 1:03:51Have you tried books and videos that just left you bored by covering topics point by point?
- 1:03:56Well, check out my online course, Python Jumpstart by building 10 apps at talkpython.fm slash
- 1:04:02course to experience a more engaging way to learn Python.
- 1:04:05And if you're looking for something a little more advanced, try my Write Pythonic Code course
- 1:04:09at talkpython.fm/pythonic.
- 1:04:12Be sure to subscribe to the show.
- 1:04:14Open your favorite podcatcher and search for Python.
- 1:04:17We should be right at the top.
- 1:04:18You can also find the iTunes feed at /itunes, Google Play feed at /play, and
- 1:04:24direct RSS feed at /rss on talkpython.fm.
- 1:04:27Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 1:04:32Corey just recently started selling his tracks on iTunes, so I recommend you check it out at
- 1:04:37talkpython.fm/music.
- 1:04:39You can browse his tracks he has for sale on iTunes and listen to the full-length version
- 1:04:43of the theme song.
- 1:04:44This is your host, Michael Kennedy.
- 1:04:46Thanks so much for listening.
- 1:04:48I really appreciate it.
- 1:04:49Smix, let's get out of here.
- 1:04:52Staying with my voice.
- 1:04:53There's no norm that I can fill within.
- 1:04:55Haven't been sleeping.
- 1:04:56I've been using lots of rest.
- 1:04:58I'll pass the mic back to who rocked it best.
- 1:05:01I'll pass the mic.
- 1:05:12Bye.
- 1:05:13.
- 1:05:13you