Latest / Talk Python To Me / #304: asyncio all the things with Omnilib
Transcript
- 0:00The relatively recent introduction of async and await as keywords in Python have spawned a whole
- 0:04area of high-performance, highly scalable frameworks and supporting libraries.
- 0:08One such library that has great async building blocks is OmniLib. On this episode, you'll meet
- 0:14John Reese. John is the creator of OmniLib, which includes packages such as aioitertools,
- 0:19aiomultiprocess, and aiosqlite. Join us as we async all the things. This is Talk Python To Me,
- 0:26episode 304, recorded February 16th, 2021.
- 0:31Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 0:49and the personalities. This is your host, Michael Kennedy. Follow me on Twitter where I'm at
- 0:54mkennedy and keep up with the show and listen to past episodes at talkpython.fm and follow the
- 0:59show on Twitter via at Talk Python. This episode is brought to you by Linode and Talk Python training.
- 1:05Please check out the offers during their segments. It really helps support the show.
- 1:09John, welcome to Talk Python To Me.
- 1:11Howdy. It's good to be here.
- 1:13Yeah, it's great to have you here as well. It's going to be a lot of fun to talk to you about
- 1:17async stuff. I think we both share a lot of admiration and love for asyncioing all the
- 1:23things.
- 1:23I definitely do. It's one of those cases where the things that it enables is so different,
- 1:29and you have to think about everything so differently when you're using asyncio,
- 1:32that it's a nice challenge, but also has potentially really high payoff if it's done well.
- 1:39Yeah, it has a huge payoff. And I think that it's been a little bit of a mixed bag in the terms of
- 1:44the reception that people have had. I know there have been a couple of folks who've written articles
- 1:48like, well, I tried it. It wasn't that great. But there's also, you know, I've had examples where I'm
- 1:53doing something like web scraping or actually got a message from somebody who listened. Maybe they were
- 1:57listening to Python Bytes, my other podcast. But anyway, I got a message from a listener after we
- 2:01covered some cool asyncio things and web scraping. They had to download a bunch of stuff.
- 2:06Like it takes like a day to literally it takes all day or something. It was really crazy. And then
- 2:10they said, well, now I'm using async and now my computer runs out of memory and crashes. It's
- 2:14getting it so fast. Like that's a large difference right there. Right. So there's certainly a category
- 2:19of things where it's amazing.
- 2:21Yeah, I think the case we've seen it most useful for is definitely doing those sorts of like concurrent
- 2:27web requests. Internally, it's also extraordinarily useful in monitoring situations where it's like you
- 2:33want to be able to talk to a whole bunch of servers as fast as possible. And maybe the amount of stuff
- 2:38that comes back from it is not as important as being able to just talk to them repeatedly.
- 2:42Yeah.
- 2:42But you're right. There's definitely a lot of cases where people are not necessarily using
- 2:46it correctly or they're hoping to like add a little bit of async into an existing thing.
- 2:50And that doesn't always work as well as just building something that's async from the start.
- 2:54Yeah. And there's more frameworks these days that are welcoming of async from the start,
- 2:59I guess. Yeah. We're going to talk. Yeah. We're going to talk about that. But before we get too
- 3:02far down the main topic, let's just start with a little bit of background on you. How'd you get
- 3:05into programming in Python?
- 3:06Sure. So my first interaction with the computer was when I was, you know, maybe like five or six years
- 3:12old. My parents had a TI-99/4A, which is like the knockoff Commodore attached to the television.
- 3:19Yeah. And I think back to that, like, how could you have like legible text on the CRT TV?
- 3:26It was pretty bad.
- 3:27It's bad, right?
- 3:28It's like my biggest memory of it is really just every time we would try to play a game and the
- 3:34cartridge or tape or whatever wouldn't work correctly, it would just dump you at a basic
- 3:39prompt where it's just expecting you to start typing some programming in. And like nobody in
- 3:44my family had a manual or knew anything about programming at the time. There was like, I think
- 3:48maybe we figured out that you could like print something to the screen, but nothing beyond that.
- 3:52And it wasn't until we ended up getting a DOS computer, you know, a few years later that really
- 3:58started to actually do some quote unquote real programming where we were writing like batch
- 4:04scripts to do menus or like, you know, deciding what program to run or things like auto exec on a
- 4:11floppy disk in order to boot into a game.
- 4:13I was just thinking of all the auto exec bad stuff that we had to do like, oh, you want
- 4:17to play Doom, but you've got you don't have enough high memory, whatever that was. And so
- 4:21you've got to rearrange where the drivers are. I mean, what's what a weird way to just I want
- 4:25to play a game. So I've got to rework where my drivers are.
- 4:28Make sure you don't load your mouse driver when you're booting into this one game that doesn't
- 4:33need the mouse because otherwise you run out of memory. Yeah, it was kind of crazy.
- 4:36And my biggest memory of programming there was there was QBasic on it and it came with
- 4:41this gorilla game where you just like throw bananas at another gorilla from like some sort
- 4:47of like city skyline.
- 4:48Like a King Kong knockoff, Donkey Kong knockoff type thing.
- 4:52Yeah, exactly. And I would struggle to figure out how that was actually doing anything.
- 4:57And he's like, I'd try to poke at it and figure it out as I went. Didn't really do that much,
- 5:01but it was actually my first opportunity for quote unquote open source projects because
- 5:06there's a video game that I really, really liked called NASCAR racing.
- 5:10And one of the things that I learned was you is on the burgeoning part of the internet for
- 5:16me, at least was people would host these mods for the game on like geo cities or whatever.
- 5:21And so these, these would change like the models for the cars or the wheels or add tracks or
- 5:26textures or whatever. And I actually wrote a batch script that would let you like at the
- 5:30time that you wanted to play the game, pick which of the ones you had enabled because
- 5:34you couldn't have them all enabled. So it would like, right.
- 5:37It's basically just a batch script that would go and like copy a bunch of files around from
- 5:41one place to another. And then when you're done with the menus or whatever, then it would
- 5:45launch the game. And I remember posting that on geo cities and, you know, having the silly
- 5:51little like JavaScript counter or whatever it was, take up to like a couple hundred page
- 5:56views of people downloading just this script to switch mods in and out. And so that was
- 6:01like the first real taste of like open source programming or open source projects that
- 6:05I had, but that actually like led into the way that I really learned programming, which was
- 6:10I wanted to have my own website that was more dynamic than what geo cities had.
- 6:14And so I ended up basically picking up Pearl and eventually PHP to write your web pages that I hosted
- 6:22on my own machine at home from like IIS and active.
- 6:26And how did you get a, what did you do? Do you use like DynDNS or something like that?
- 6:31Yes, exactly. Dying DNS. It was the jankiest setup, but it at least worked and I could impress
- 6:37my friends and it wasn't until I got to college and I was working on my first internship where
- 6:43the main project I was working on was essentially improving an open source bug tracker written
- 6:49in PHP in order to make it do the things that my company wanted to be able to do in it. So
- 6:55like adding a plugin system and things like that. And in the process of that, they, I eventually
- 7:00became a maintainer of the project and they had a bunch of Python scripts for managing releases,
- 7:05like doing things like creating the release tar balls, running other sort of like linter type
- 7:10things over the code base. And that was my very first taste of Python. And I hated it because it
- 7:16was just like, I couldn't get past the concept of like, you're forcing me to do white space.
- 7:21Like how barbaric is this? But it actually didn't take long before I realized that that actually makes
- 7:27the code more readable. It's like, you can literally pick up anybody else's Python script and it looks
- 7:32almost exactly like how you would have done it yourself.
- 7:34Yeah. And you've got a lot of the PEP 8 rules and tools that automatically re, you know,
- 7:39format stuff into that. So it's very likely, you know, you've got black and PyCharm's reformat
- 7:45and whatnot, right?
- 7:46This was all before that. So I think this was when like Python 2.6 was the latest.
- 7:50This was quite a while ago.
- 7:52Right before the big diversion.
- 7:53Yeah, yeah, exactly. Like I had no idea what Python 3 was until like 3.2 or 3.3 came out because
- 8:00it was just sequestered in this world of writing scripts for whatever version of Python was on
- 8:05my Linux box at the time.
- 8:06Right. You know, I suspect in the early days, probably the editors were not as friendly or
- 8:10accommodating, right? Like now, if you work with PyCharm or VS Code or something, you just write
- 8:15code and it automatically does the formatting and the juggling and whatnot. And once you get used to it,
- 8:20you don't really think that much about it. It just magically happens as you work on code.
- 8:23I'm wanting to say at the time I was just doing something stupid like Notepad++ or,
- 8:29you know, one of the other like really generic free text editors.
- 8:32Like Notepad, but Consolas fonts.
- 8:34Or it was Eclipse. It might have been Eclipse.
- 8:36Yeah. Was it maybe PyDev?
- 8:38I don't think I ever use a Python specific editor.
- 8:42Like, yeah, I think I've tried PyCharm exactly once and I do just enough stuff that's not Python
- 8:48that I don't want to deal with an IDE or editor that's not generalized.
- 8:53Right. Sure. Makes sense.
- 8:54Speaking of stuff you work on, what do you do day to day? What kind of stuff do you do?
- 8:59I'm a production engineer at Facebook on our internal Python foundation team. And so most of
- 9:06what I do there is, you know, building infrastructure or developer tools, primarily enabling engineers,
- 9:12data scientists, and AI or ML researchers to do what they do in Python every day. So some of that is like
- 9:21building out the system that allows us to integrate open source third party packages into the Facebook
- 9:27repository. Some of that is literally developing new open source tools for developers to use.
- 9:33A while back, I built a tool called called Bowler that is basically a refactoring tool for Python. It's based off of lib2to3 that's in, you know, open source Python essentially gives you a way to make safe code modifications rather than using regular expressions, which are terrible.
- 9:52Yeah, for sure. And based on like the AST or something like that. Yeah, exactly. Yeah. Okay.
- 9:56And it's like the benefit of LibCST is that it takes in the concrete syntax tree. So it keeps track of all the white space comments and everything else. So that if you modify the tree in lib2to3, it will then allow you to write that back out exactly the way the file came in. Whereas the AST module would have thrown all of that, you know, metadata away.
- 10:18Right, right. Formatting spaces, whatever. It doesn't care.
- 10:20Yeah. And one of the newer projects I've worked on is called USort and it's microsort. Essentially, it's a replacement that we're using internally for isort because isort has some potentially destructive behaviors in its default configuration. And our goal was essentially to get import sorting done in a way that does not require adding comment directives all over the place.
- 10:45Right, right, right, right.
- 11:15And you can't just put a skip directive on that function call, because that just means isort won't try to sort that one, but it'll sort everything else around it as well. And so what we ended up seeing was a lot of developers doing things like isort skip file, and just turn off import sorting altogether.
- 11:31One of the things of isort is like, first do no harm. It's trying its best to make sure that these common use cases are just treated normally and correctly from the start. In most cases, it's a much safer version of isort. It's not complete. It's not a 100% replacement, but it's the thing we've been using internally. And it's one of the cases where I'm proud of the way that we are helping to build better tools for the ecosystem.
- 11:56Yeah, this is really, I never really thought about that problem. One thing that does drive me crazy is sometimes I'll need to change the Python path, so that future imports, regardless of your working directory behave the same if you don't have a package or something like that, right? Something simple.
- 12:11That's super common in the AI and ML type of workflows.
- 12:14Yeah, and I get all these warnings, like, you should not have code that is before an import. Like, well, but this one is about making the import work. If I don't put this, it's going to crash for some people if they run it weirdly and stuff like that, right?
- 12:25Yeah.
- 12:25Interesting. Yeah, very, very cool project. Nice. All right. So let's dive into async, huh?
- 12:30Sure.
- 12:30Yeah. So maybe a little bit of history. You know, Python, it's hard to talk about asynchronous programming in Python without touching on the gill, global interpreter lock, normally spoken as a bad word, but it's not necessarily bad. It has a purpose. It just its purpose is somewhat counter to making asynchronous code run really quick and in parallel.
- 12:49I mean, it's one of those things where if you imagined what Python would be without the global interpreter lock, you end up having to do a lot more work to make sure that, let's say, if you had multi-threaded stuff going on, you'd have to do a lot more work to make sure that they're not clobbering some shared data. Like, you look at the way that you have to have synchronizations and everything else in Java or C++.
- 13:09We don't generally need that in Python because the GIL prevents a lot of that bad behavior. And the current efforts to kind of remove the GIL that have been ongoing for the past eight to 10 years, in every single case, once you remove that GIL and add a whole bunch of other locks, the whole system is actually slower.
- 13:26So this is one of those things where it's like, it does cause problems, but it also enables Python to be a lot faster than it would be otherwise.
- 13:34And probably simpler.
- 13:35Yeah.
- 13:35Yeah. So the global interpreter lock, when I first heard about it, I thought of it as a threading thing and it sort of is, but you know, it's primarily says, let's create a system so that we don't have to do locks as we increment and decrement the ref count on variables. So basically all the memory management can happen without the overhead of taking a lock, releasing a lock, all that kind of weirdness.
- 13:56Yeah.
- 13:56So we've got like a bunch of early attempts. I mean, we've got threading and multiprocessing have been around for a while. There's gevent, Tornado, but then around, I guess, was it?
- 14:05Python 3.4. We got asyncio, which is a little bit of a different flavor than, you know, like the computational threading or the computational multiprocessing side of async.
- 14:16It's actually an interesting kind of throwback to the way that computing happened in like the 80s and early 90s, where like Windows 3.1 or classic macOS, essentially you can, you know, run your program or your process and you actually have to cooperatively give up control of the CPU in order for another program to work.
- 14:37So there'd be a lot of cases where, like, if you had a bad behaving program, you'd end up not being able to do multitasking in, you know, these old operating systems because it was all cooperative.
- 14:47In the case of asyncio, it's essentially taking that mechanism where you don't need to do a lot of context switching in threads or in processes.
- 14:57And you're essentially letting a bunch of functions cooperatively coexist and essentially say when your function gets to a point where it's doing a network request and it's waiting on that network request,
- 15:08your function then will nicely hand over control back to the asyncio framework, at which point the framework and event loop can go find the next task to work on that's not blocked on something.
- 15:20Yeah. And it's very often doesn't involve threads at all or, you know, the one main thread, right?
- 15:24Like, so, yeah, you know, it's not a way to create threading.
- 15:27It's a way to allow stuff to happen while you're otherwise waiting.
- 15:31Yeah. In the best case, you only ever have the one thread.
- 15:34And now in reality, it doesn't work like that because a lot of our, you know, modern computing infrastructure is not built in an async way.
- 15:41So like if you look at file access, there's basically no real way to do that asynchronously without threads.
- 15:47But in the best case, like network requests and so forth, if you have the appropriate hooks from the operating system, then that can all be completely in one thread.
- 15:55And that means you have a lot less overhead from the actual runtime and process from the operating system because you're not having to constantly throw a whole bunch of memory onto a stack and then pull off memory from another stack and try to figure out where you were when something interrupted you in the middle of 50 different operations.
- 16:16This portion of Talk Python To Me is sponsored by Linode.
- 16:19Simplify your infrastructure and cut your cloud bills in half with Linode's Linux virtual machines.
- 16:23Develop, deploy, and scale your modern applications faster and easier.
- 16:27Whether you're developing a personal project or managing large workloads, you deserve simple, affordable, and accessible cloud computing solutions.
- 16:35As listeners of Talk Python To Me, you'll get a $100 free credit.
- 16:39You can find all the details at talkpython.fm/Linode.
- 16:43Linode has data centers around the world with the same simple and consistent pricing regardless of location.
- 16:49Just choose the data center that's nearest to your users.
- 16:52You'll also receive 24-7, 365 human support with no tiers or handoffs regardless of your plan size.
- 16:58You can choose shared and dedicated compute instances, or you can use your $100 in credit on S3-compatible object storage, managed Kubernetes clusters, and more.
- 17:09If it runs on Linux, it runs on Linode.
- 17:11Visit talkpython.fm/Linode or click the link in your show notes, then click that create free account button to get started.
- 17:18Right, if it starts swapping out the memory it's touching, it might swap out what's in the L1, L2, L3 caches.
- 17:27Yeah, exactly.
- 17:28It can have huge performance impacts, and it's just constantly cycling back and forth out of control a lot of times, right?
- 17:34Yeah.
- 17:34In a lot of our testing internally when I was working on things that would talk to lots and lots of servers,
- 17:39it's like we would hit a point where somewhere between 64 and 128 threads would actually start to see less performance overall
- 17:48because it just spends all of its time trying to context switch between all of these threads.
- 17:53Right, right, right.
- 17:54You're interrupting these threads at an arbitrary point in time because the runtime is trying to make sure that all of the threads are serviced equally.
- 18:01But in reality, like half of these threads don't need to be given the context right now.
- 18:06So by doing those sort of interrupts in context, which is when the runtime wants to rather than when the functions or requests are wanting to,
- 18:14you end up with a lot of suboptimal behavior.
- 18:17Yeah, interesting.
- 18:17Yeah, and also things like locks, mutexes, and stuff don't work in this world because it's about what thread has access.
- 18:26Well, all the codes on one thread.
- 18:27So to me, the real zen of AsyncIO, at least for many really solid use cases, kind of like we touched on,
- 18:33is it's all about scaling when you're waiting.
- 18:36Yes.
- 18:36If I'm waiting on something else, it's like completely free to just go to it.
- 18:40If I'm calling microservices, external APIs, if I'm downloading something or uploading a file or talking to a database,
- 18:48or even maybe accessing a file with something like aiofiles.
- 18:51Yeah.
- 18:52Yeah.
- 18:52Yeah, there's a cool place called AI Awesome AsyncIO by TML Fear.
- 18:57It's pretty cool.
- 18:58Have you seen this place?
- 18:59I have looked at it in the past.
- 19:01I end up spending so much time looking at and building things.
- 19:04It's like I haven't actually gotten a lot of opportunity to use a bunch of these.
- 19:09Most of my time, I'm actually not working that high enough on the stack to make use of them.
- 19:13Right, right, right.
- 19:14These are a lot of more frameworks.
- 19:15We do have some other neat things in there as well, like asyncssh.
- 19:19I hadn't heard of that one.
- 19:20But anyway, I'll put that in the show notes.
- 19:21That's got, I don't know, 50, 60 libraries and packages for solving different problems with AsyncIO, which is pretty cool.
- 19:27Yeah.
- 19:28Whenever I talk about AsyncIO, one of the things I love to give a shout out to is this thing called Unsync.
- 19:32Have you heard of Unsync?
- 19:34I had not heard about it until I looked at the show notes.
- 19:36But it sounds a lot like some of the things that I've seen people implement a lot of different cases.
- 19:42It's a very filling a common sort of use case where you have, like I was saying earlier, where people want to mix AsyncIO into an existing synchronous application.
- 19:50You do have to be very careful about how you do that, especially vice versa.
- 19:54Or a lot of the stumbling blocks we've seen tend to be cases where you have synchronous code that calls some Async code that then wants to call some synchronous code, but on like another thread so that it's not blocked by it.
- 20:08And you actually end up getting this like in-out sort of thing where you have like nested layers of AsyncIO.
- 20:15I'm not sure how much this may or may not solve that.
- 20:18I think this actually helps some with that as well.
- 20:21Basically, the idea is there's two main things that it solves that I think is really neat.
- 20:24One, it's like a unifying layer across multiprocessing, multithreading, and straight AsyncIO.
- 20:30Right?
- 20:30So you put a decorator onto a function.
- 20:32If the function is an Async function, it runs it on AsyncIO.
- 20:36If it's a regular function, it runs it on a thread.
- 20:38And if you say it's a regular function, but it's computational, it'll run it on multiprocessing.
- 20:43But it gives you basically an AsyncIO, Async and await API for it.
- 20:46And it figures out how to run the loop and all.
- 20:48Anyway, it's pretty cool.
- 20:49Not what we're here to talk about, but it's definitely worth checking out while we're on the subject.
- 20:53Ultimately, it gives you just a future that you can then either await or ask for the result from, right?
- 20:59Yeah, exactly.
- 20:59Exactly.
- 21:01And the result, instead of saying, you've got to wait until it's finished before you can get the result,
- 21:04you just go, give me the result.
- 21:06And if it needs to, it'll just block.
- 21:07So it's a nice way to sort of cap the AsyncIO.
- 21:10You know, like one of the challenges of AsyncIO is, well, five levels down the call stack,
- 21:14this thing wants to be Async.
- 21:15So the next thing's Async.
- 21:17So the next thing up is Async.
- 21:18And like all of a sudden, everything's Async, right?
- 21:20And so it was something like this.
- 21:22I mean, you could do it yourself as well.
- 21:23You can like just go create an event loop, run it.
- 21:26And at this level, we're not going to be Async above it.
- 21:29But we're coordinating stuff below using AsyncIO.
- 21:31And here's where it stops.
- 21:33Yeah.
- 21:33It sounds like a nicer version of what I see dozens of when you have lots and lots of engineers
- 21:38that aren't actually working on the same code base together, but they're all in the same
- 21:42repository.
- 21:42And we end up seeing these cases where everybody has solved the same use case.
- 21:46So I do think this would be useful.
- 21:48And I'm actually planning on sharing it with more people.
- 21:51Yeah.
- 21:51Yeah.
- 21:51Check it out.
- 21:51It's like a subtotal, I think, 126 lines of Python in one file.
- 21:56But it's really cool, this unifying API.
- 21:58All right.
- 21:58I guess that probably brings us to OmniLib.
- 22:01You want to talk about that for a little bit?
- 22:03So this is what I thought would be fun to have you on the show to really focus on is like
- 22:07AsyncIO, but then also you've created this thing called OmniLib, the OmniLib project that
- 22:12has solves four different problems with AsyncIO.
- 22:15And obviously you can combine them together, I would expect.
- 22:18The origins of this really is like I had built the like aiosqlite was the first thing that
- 22:24I wrote that was an Async framework.
- 22:26And then I'd built a couple more.
- 22:28And at one point I realized these projects are actually getting really popular and people
- 22:32are using it, but they're just like one of the hundred things that are on my GitHub profile
- 22:38and graveyard.
- 22:38So I really felt like they needed to have their own separate place for like, these are the
- 22:43projects that I'm actually proud of.
- 22:45I thought that was actually a good opportunity to be able to make a dedicated like project
- 22:50or organization for it.
- 22:53And essentially say that everything under this I'm guaranteeing is going to be developed under
- 22:59a very inclusive code of conduct that I personally believe in and want to try and also at the same
- 23:05time make it more welcoming and supportive.
- 23:07You know, other contributors, especially newcomers or other otherwise marginalized developers in
- 23:14the ecosystem and try to be as friendly as possible with it.
- 23:17And it's like, this is something that I tried to do beforehand and I just never really formalized
- 23:21it on any of my projects other than like, here's a code of conduct file in the repository.
- 23:26Yeah.
- 23:27Yeah.
- 23:27But this is like really one of the first times where I wanted to put all these together and
- 23:32make sure that these are really like, this is going to be whether or not enough people
- 23:36make it a community.
- 23:37I want it to be welcoming from the outset.
- 23:40Right.
- 23:40That's really cool.
- 23:41And you created your own special GitHub organization that you're putting it all under and stuff like
- 23:45that.
- 23:46So it's kind of the things that are graduated from your personal project.
- 23:49Is that a story?
- 23:50Yeah.
- 23:50And kind of the threshold I tried to follow is like, if this is worth making a Sphinx documentation
- 23:56site for, then it's worth putting on, you know, OmniLib project.
- 23:59So they're not all asyncio.
- 24:01That just happens to be where a lot of my interests and utility stands at.
- 24:06So that's what most of them are, or at least the most popular ones.
- 24:10But there are other projects that I have also in the back burner that will probably end up
- 24:15there that maybe not as useful libraries or whatever, but either way, like I said, these
- 24:20are the ones that I'm at least proud of.
- 24:21Nice.
- 24:22That's cool.
- 24:22So you talked about the being there to support people who are getting into open source and
- 24:27whatnot and having that code of conduct.
- 24:29What other than that, is there like a mission behind this?
- 24:32Like I want to make this category of tools or solve these types of problems, or is it just
- 24:37these are the things that you've graduated?
- 24:39It's something I've tried to think about.
- 24:41I'm not 100% certain.
- 24:42I would like it to have maybe more of a mission, but at the same time, it's like, especially
- 24:48from things I've had to deal with at work, it's like, I don't want this to be a dumping
- 24:51ground of stuff either.
- 24:52Like I want this specifically, it's like, like in the opening statement, I want it to be a group
- 24:57of high quality projects that are, you know, following this code of conduct.
- 25:00So from that perspective, it's like, at the moment, it's like my personal interests are always
- 25:06in building things where I find gaps in, you know, availability from other libraries.
- 25:11So that's probably the closest to a mission of what belongs here is just things that haven't
- 25:16been made yet.
- 25:17Yeah.
- 25:17Yeah.
- 25:18Yeah.
- 25:18But either way, I just want to have that, you know, dedication to the statement of like,
- 25:22I want these to be high quality.
- 25:24I want them to be tested.
- 25:25I want them to be, you know, have continuous integration and testing and well-documented
- 25:30and so forth.
- 25:31Yeah.
- 25:31Super cool.
- 25:32All right.
- 25:32So there's four main projects here on the homepage.
- 25:36I mean, do you have the attribution one, but that's...
- 25:39Like helper tool.
- 25:40Exactly.
- 25:41Let's talk about the things that maybe they're the AIO extension of.
- 25:45So in Python, we have iter tools, right?
- 25:48Which is like tools for easily creating generators and such out of collections and whatnot.
- 25:55So you have aioitertools, which is awesome.
- 25:57And then we have multiprocessing, which is a way around the GIL.
- 26:00It's like, here's a function and some data, go run that in a sub process and then give me
- 26:04the answer.
- 26:04And because it's a sub process, it has its own sub GIL or its own separate GIL.
- 26:08So it's all good.
- 26:09So you have aiomultiprocess, which is cool.
- 26:11And then one of the most widely used databases is SQLite, already built into Python, which
- 26:18is super cool.
- 26:18And so you have aiosqlite.
- 26:20And then sort of extending that, that's like a raw SQLite, you know, raw SQL library that's
- 26:25asynch.io.
- 26:26Then you have AQL, which is more ORM-like.
- 26:29I'm not sure it's 100% ORM.
- 26:31You could categorize it for us, but it's like an ORM.
- 26:34Yeah, I've definitely used like in quotes, in scare quotes, ORM-like.
- 26:39Because I want it to be able to essentially be a combination of like well-typed table definitions
- 26:46that you can then use to generate queries against the database.
- 26:50As of right now, it's more like writing a, like a DSL that lets you write a backend agnostic
- 26:57SQL statement.
- 26:58Right.
- 26:59Okay.
- 26:59Yeah.
- 27:00DSL domain specific language for people who aren't entirely sure.
- 27:02Yeah.
- 27:03So really it's essentially just stringing together a whole bunch of method calls on
- 27:07a table object in order to get a SQL query out of it.
- 27:11The end goal is to be able to have that actually be a full end-to-end thing where you've defined
- 27:17your tables and you get objects back from it.
- 27:19And then you can like call something on the objects to get them to update themselves back
- 27:23into a database.
- 27:24But I've been very hesitant to pick an API on it for how to actually get all that done
- 27:29because trying to do that in an async fashion is actually really difficult to do it right.
- 27:34And separately, like trying to do asyncio and have everything well-typed, you know, it's
- 27:40like two competing problems that have to be solved.
- 27:43Yeah.
- 27:44I just recently started playing with SQLAlchemy's 2.0, 1.4 beta API where they're doing the
- 27:51async stuff and it's quite different than the traditional SQLAlchemy.
- 27:54So yeah, you can see the challenges there.
- 27:57And it's also a case where it's like having something to generate the queries to me is more
- 28:02important than having the thing that will actually go run the query, especially for a lot of internal
- 28:06use cases.
- 28:06We really just want something that will generate the query or we already have a system that
- 28:11will talk to the database once you give it a query and parameters.
- 28:14It's the piece of actually saying, defining what your table hierarchy or structure is,
- 28:20and then being able to run stuff to get the actual SQL query out of it, but have that work
- 28:26for both SQLite and MySQL or Postgres or whatever other backend you're using.
- 28:32Having it be able to use the same code and generate the correct query based off of which
- 28:37database you're talking to is the important part.
- 28:39Yeah, cool.
- 28:40Well, there's probably a right order to dive into these, but since we're already talking
- 28:43about the AQL one a lot, maybe give us an example of what you can do with it.
- 28:48Maybe talk us through, it's hard to talk about code on air, but just give us a sense of what
- 28:52kind of code you write and what kind of things it does for us.
- 28:55This is heavily built around the idea of using data classes.
- 29:00In this case, it specifically uses attrs simply because that's what I was more familiar
- 29:04with at the time that I started building this.
- 29:06But essentially, you create a class with, you know, essentially, all of your columns specified
- 29:12on that class with the name and the type.
- 29:15Not like SQL.
- 29:16But not like super heavy.
- 29:17Or Django style.
- 29:18Yeah, exactly.
- 29:19Like native types like id colon int, name colon str, not sa.column dot, you know, sa.string
- 29:25and so on, right?
- 29:25Yeah, exactly.
- 29:26Like I want this to look as close to a normal data class definition as possible and essentially
- 29:32be able to decorate that.
- 29:33And you get a special object back that when you use methods on it, like in this case, the
- 29:39example is you're creating a contact.
- 29:41So you list the integer id, the name of it, and the email.
- 29:45And whatever the primary key doesn't really matter in this case.
- 29:48Whether the id ends up getting auto incremented, again, doesn't really matter.
- 29:53What we're really worried about is generating the actual queries.
- 29:56And you're assuming like somebody's created the table.
- 29:58It's already got a primary key for id.
- 30:01It's auto incrementing or something like that.
- 30:03Yeah.
- 30:03And you just want to talk to the thing.
- 30:04Yeah.
- 30:07Talk Python To Me is partially supported by our training courses.
- 30:10Do you want to learn Python, but you can't bear to subscribe to yet another service?
- 30:15At Talk Python Training, we hate subscriptions too.
- 30:18That's why our course bundle gives you full access to the entire library of courses for
- 30:23one fair price.
- 30:24That's right.
- 30:25With the course bundle, you save 70% off the full price of our courses and you own them
- 30:30all forever.
- 30:31That includes courses published at the time of the purchase, as well as courses released within
- 30:36about a year of the bundle.
- 30:37So stop subscribing and start learning at talkpython.fm/everything.
- 30:44And so essentially you take this contact class that you've created and you can call a select
- 30:51method on it that will then, you know, you can add an aware method to decide which contacts
- 30:57you want to select.
- 30:58There's other methods for changing the order or limits or furthermore, if you wanted to do
- 31:03joins or other sorts of things.
- 31:05It kind of expects that you know what general SQL syntax looks like because you string together
- 31:12a bunch of stuff kind of in the same order that you would with a SQL query.
- 31:16But the difference is that in this case, like when you're doing the where clause, rather than
- 31:20having to do an arbitrary string that says, you know, column name like, and then some string
- 31:26literal, in this case, you're saying like where contact dot email dot like, and then passing
- 31:32the thing that you want to check against.
- 31:34And the other alternative is you could if you wanted to look for a specific one, you could
- 31:39say like where contact dot email equals equals, and then the value you're looking for.
- 31:44And so you're kind of using or abusing Python's expression syntaxes to essentially build up
- 31:52your query, definitely using a domain specific language in this case.
- 31:56But essentially having the fluent API, once you string all this together, you have this query
- 32:01object, which you can then, you know, pass to the appropriate engine to get an actual finalized
- 32:08SQL query and the parameters that would get passed if you were doing a prepared query.
- 32:12But you could also potentially like in the future, the goal was you would also be able
- 32:17to make manage your connection with a QL and basically be able to tell it to run this query
- 32:24on that connection.
- 32:25And regardless, you'd be able to do this the same with SQLite or MySQL or whatever.
- 32:29And the library is the part that handles deciding what specific part of the incompatible SQL languages
- 32:38that they all use will actually be available.
- 32:41Right.
- 32:41Yeah.
- 32:42Like, for example, MySQL uses question mark for the parameters.
- 32:45Yeah.
- 32:46SQL server uses, I think, at parameter name.
- 32:49There's like, they all have their own little style.
- 32:51It's not the same, right?
- 32:52Yeah.
- 32:53And some of that is kind of moot because of the fact that the most of the engine libraries
- 32:59that we use commonly in Python, like AIO, MySQL or SQLite or whatever, they're already kind
- 33:05unified around the, there's a specific PEP that defines what the database interface is going to look like.
- 33:13Right.
- 33:13The DB API 2 or whatever.
- 33:15Yes, that.
- 33:16So some of that work has already been done by the PEPs and by the actual database engines.
- 33:21But there's a lot of cases where it's a little bit more subtle, like the semantics, especially around using a like expression.
- 33:28MySQL does a case insensitive matching by default, but SQLite doesn't.
- 33:35AQL tries to kind of like unify those where possible.
- 33:39But also there's cases, especially when you're getting into joins or group buys, things like that, where the actual specific syntax being used will start to vary between the different backends.
- 33:50And that's where we've had more issues, like especially the whole point of SQLite for a lot of people is as a drop in replacement to MySQL when you're running your unit tests.
- 33:58And so you want your code to be able to do the same thing regardless of what database engine it's connected to.
- 34:03And this is one way to do that.
- 34:04Okay, that's cool.
- 34:05Yeah, with SQLite, you can say the database lives in, you know, colon memory.
- 34:09Yeah, exactly.
- 34:10And then you can just tear it up for your unit tests and then it just goes away.
- 34:14Nice.
- 34:14So maybe that brings us to the next one, the aiosqlite.
- 34:18Sure.
- 34:19This one is all about async.io.
- 34:20You can see from the example here.
- 34:22You want to tell us about that?
- 34:23Yeah, this was again, born out of, you know, a need for using SQLite, especially in testing frameworks and so forth to replace my SQL.
- 34:32Cool.
- 34:33And essentially what I was doing was taking the normal SQLite API from Python and essentially saying like, how would this look in an asyncio world?
- 34:44Like if we were re-implementing SQLite from the ground up in an asyncio world, how can we do that?
- 34:50And essentially, so in this case, we're heavily using async context managers and awaitables in order to actually run the database connection to SQLite on a separate thread and provide as much of an async interface to that as possible.
- 35:08So when you connect to aiosqlite, it spawns a background thread that actually uses the standard SQLite library to connect to your database.
- 35:18And then it has methods on that thread object that allow you to actually make calls into that database.
- 35:25And those are essentially proxied through futures.
- 35:29So if you want to execute a query, when you await that query execution, it will basically queue the function call on the other thread and basically tell it, here's the future to set when the result is ready.
- 35:45So once the SQLite execution or cursor or whatever has actually completed doing what it's supposed to do on that background thread, it then goes back to the original threads event loop and says, you know, set this future to finished.
- 35:59And so that allows the thing originally awaiting it to actually come back and do something with the result.
- 36:06Yeah, it sounds a little tricky, but also super helpful.
- 36:08And people might be thinking, didn't we just talk about the GIL and how threading doesn't really add much?
- 36:14But when you're talking over the network or you're talking to other things, a lot of times the GIL can be released while you're waiting on the internal SQLite or something like that, right?
- 36:22Yeah.
- 36:22So the internal SQLite library on its own will release the GIL when it's calling into the underlying SQLite C library.
- 36:31And that's where it's waiting.
- 36:32So that's good.
- 36:32Yeah.
- 36:33The other side of this is that it's one thread.
- 36:36I'm not really aware of anybody who's opening, you know, hundreds of simultaneous connections to a SQLite database.
- 36:43The way that people expect to do with, say, like aiohttp or things like that.
- 36:48So while it is, you know, potentially less efficient, if you wanted to do a whole bunch of parallel SQLite connections, the problem really is that SQLite itself is not thread safe.
- 36:59So it has to have a dedicated thread for each connection.
- 37:02Otherwise, you risk corruption of the backing database.
- 37:06Which sounds not good.
- 37:07Right.
- 37:07Yeah.
- 37:07It's like, basically, you end up either where two threads clobber each other or more specifically, what SQLite says is, if you absolutely try to talk to a connection from a different thread, the Python module will complain unless you've specifically told it, no, please don't complain.
- 37:24I know it's unsafe, at which point SQLite will be really upset if you try to do a write or modification to that database.
- 37:31So there are layers of protections against that, but it is one of the underlying limitations that we have to deal with in this case.
- 37:37So if you wanted to have simultaneous connections to the same database, you really have to spin up multiple threads in order to make that happen safely.
- 37:45You could always do some kind of thread pool type thing, like we're only going to allow eight connections at a time and you're just going to block until one of those becomes free and finished or whatever, right?
- 37:55It's definitely a tricky thing.
- 37:57So like the expected use case with aiosqlite is that you'll share the database connection between multiple workers.
- 38:04So you'll like in the piece of your application that starts up, it would make the connection to the database and store that somewhere and then essentially pass that around.
- 38:13And so aiosqlite is basically expecting to use a queue system to say whoever gets the query first is the one that, you know, gets to run it first.
- 38:22And whoever asked for the query second, you know, is the second one to get it.
- 38:26So you're still doing it all on one thread and it's slightly less performant that way, but it's at least safe and still asynchronous at least.
- 38:33Yeah, that's good.
- 38:33Very nice.
- 38:34And one of the things that looking at your example here, which I'll link in the show notes, of course, is Python has a lot of interesting constructs around async and await.
- 38:43You know, a lot of languages, you know, you think C# or JavaScript or whatever.
- 38:47It's kind of async function await function calls.
- 38:50We're good.
- 38:51But, you know, we've got async with, async for, a lot of interesting extensions to working with async and other constructs.
- 38:58Yeah, it actually makes it really nice in some ways.
- 39:01And essentially these are just syntactic wrappers around a whole bunch of magic methods on objects.
- 39:07Right.
- 39:07Like await thing, enter, do your thing, right?
- 39:10Then await exit.
- 39:11Right.
- 39:12The nice part is that for some amount of extra work in the library, setting up all those magic methods everywhere and deciding, you know, the right way to use them.
- 39:21The benefit at the end is that you have this very simple syntax for asynchronously iterating over the results of a cursor.
- 39:28In that case, you don't have to care that after, you know, 64 elements of iteration, you've exhausted the local cache and now SQLite has to go back and fetch the next batch of 64 items.
- 39:40In that case, it's like that's transparent to your application.
- 39:43And that's where the coroutine that's iterating over that cursor would then hand back its control of the event loop.
- 39:50And the next coroutine in waiting essentially is able to then, you know, wake up and go do its own thing, too.
- 39:56Oh, how cool.
- 39:57I didn't even really think of it that way.
- 39:58That's neat.
- 39:58Maybe next one to touch on would be aiomultiprocess.
- 40:02Sure.
- 40:03It just now crossed a thousand stars today or recently.
- 40:06Oh, yeah.
- 40:07Yeah, it did.
- 40:07Yeah, very recently.
- 40:08That's awesome.
- 40:08That's my real pride and joy here is getting all those stars.
- 40:11There's this interesting dichotomy set up between threading and multiprocessing in Python.
- 40:16So with multi-threading, you're able to interleave execution.
- 40:20So with the gil, it means that only one thread can actually be modifying Python objects or running Python code at any given time.
- 40:28So you're essentially limited to one core of your CPU.
- 40:31And these days, that's a big limitation, right?
- 40:33Right, right.
- 40:34Exactly.
- 40:34Like I see servers on a regular basis that are like 64 to 100 cores.
- 40:39So only using one of them is basically a non-starter.
- 40:42You get a lot of people with pitchfork saying, why aren't we using Rust?
- 40:46And so essentially what the alternative of this multiprocessing, where you're spinning up an individual process and each has its own gil,
- 40:53this does allow you for CPU intensive things to basically use all of the available cores on your system.
- 41:01So if you're crunching a whole bunch of numbers with NumPy or something like that,
- 41:05you could use multiprocessing and saturate all of your cores with no problem.
- 41:10In this case, essentially what happens is it spawns a child process or forks the child process on Linux.
- 41:16And then it uses a pickle module in order to send data back and forth between the two.
- 41:21And this is great.
- 41:23And it's really transparent.
- 41:24So it's super easy to write code for multiprocessing and make use of that.
- 41:29But the issue becomes if you have a whole bunch of really small things, you start to have a big overhead with pickling of the data back and forth.
- 41:36Right.
- 41:36And the coordination back and forth is like really challenging, right?
- 41:40Yeah.
- 41:40So like if you're pickling a whole bunch of smaller objects, you actually end up with a whole bunch of overhead from the pickle module where you're serializing and deserializing and creating a bunch of objects and, you know, synchronizing them across those processes.
- 41:53But the real problem is when you start to want to do things like network requests that are IO bound.
- 42:00In an individual process, like with multithreading, you could probably do 60 to 100 simultaneous network requests.
- 42:07Right.
- 42:07But you guys maybe have more than 60 servers or something.
- 42:10Sure.
- 42:10Right.
- 42:11But like if you're trying to do this with multiprocessing instead, where you have like a process pool and you give it a whole bunch of stuff to work on, each process is only going to work on one request at a time.
- 42:21So you might spin up a process and it waits for a couple seconds while it's doing that network request and then it sends it back and you haven't really gained anything.
- 42:29So if you actually really want to saturate all your cores, now you need a whole bunch more processes.
- 42:34And that then has the problem of a lot of memory overhead.
- 42:37Because even if you're using copy on write semantics with forking, the problem is that like Python goes and touches all the ref counts on everything and immediately removes any benefit of copy on write forked processes.
- 42:49Right.
- 42:49Which might do like the shared memory, right?
- 42:51So if I create 10 of these things, like 95% of the memory just might be one copy.
- 42:55But if you start touching ref counts and all sorts of stuff, you know, Instagram went so far as to disable the garbage collector.
- 43:01Right.
- 43:02To prevent that kind of stuff.
- 43:03It was insane.
- 43:03Yeah.
- 43:04So it turns out that if you fork a process, as soon as you get into that new process, Python touches like 60 to 70% of the objects and it's in the pool of memory, which basically means it now has to actually copy all of the memory from all of those objects.
- 43:20And so you don't actually get to share that much memory between the child and the parent process in the first place.
- 43:26So if you try to spin up, you know, a thousand processes in order to saturate 64 cores, you are wasting a lot, a lot of memory.
- 43:35So that's where I kind of built this piece of aiomultiprocess, where essentially what it's doing is it's spinning up a process pool and it only spins up one per core.
- 43:46And then on each child process, it then also spins up an asyncio event loop.
- 43:51Right.
- 43:51And rather than giving a normal synchronous function as the thing that you're mapping to a whole bunch of data points, you give a coroutine.
- 43:59And in this case, what aiomultiprocess is capable of doing is essentially keeping track of how many in-flight coroutines each child process is executing.
- 44:09And essentially being able to say that, like, if you wanted to have 32 in-flight coroutines per process and you had 32 processes, then of course you have whatever 32 times 32 is.
- 44:22I can't do that in my head because I'm terrible at math.
- 44:25Essentially, you get, you know, the cross product of those two numbers.
- 44:31And that's the number of actual concurrent things that you can do on aiomultiprocess.
- 44:35So the idea is like, instead of creating a whole bunch of one-off run this thing with these inputs over there, you say, well, let's create a chunk.
- 44:45Like, let's go 32 here, 32 there and run them, but do that in an async way.
- 44:50So you're scaling the wait times.
- 44:52Yeah, exactly.
- 44:53Anyway, right.
- 44:54Because you're probably doing network stuff.
- 44:55Yeah.
- 44:56And the benefit of this is essentially like you're scaling the benefits of asyncIO with the benefits of multiprocessing.
- 45:03So for math, that's easier for me to figure out.
- 45:07In reality, what we've seen is that you can generally do somewhere around 256 concurrent network requests on asyncIO on a single process before you really start to overload the event loop.
- 45:20Have you looked at some of the other event loop implementations like uvloop or any of those alternate event loop functions?
- 45:28So uvloop can make things faster, but the things that it makes faster are the parts that process like network request headers.
- 45:35The real problem at the end of the day is that the way that the asyncIO framework and event loops work is that for each task that you give them,
- 45:43it basically adds it to a round robin queue of all of the things that it has to work on.
- 45:48So at the end of the day, if you want to run a thousand concurrent tasks, that's a thousand things that it has to go through in order before it gets to any one task.
- 45:57Right.
- 45:57And it's going around asking, are you done?
- 45:59Are you done?
- 45:59Yeah.
- 45:59Or something like that, basically.
- 46:01And if you're doing anything with the result of that network request before you actually return the real result from your coroutine,
- 46:09then you're almost certainly going to be starving the event loop of or starving other coroutines on the same event loop of processing power.
- 46:17And so what we've seen actually is you end up with cases where you technically time out the request because it's taken too long for Python or asyncIO to get back to the network request before it hits like a TCP interrupt or something like that.
- 46:32That's interesting.
- 46:32Yeah.
- 46:33So this way you could say like, well, throw 10 processes or 20 processes at it and make that shorter.
- 46:38If you're willing to run 256 network requests per process and you have 10 processes or 10 cores, then suddenly you can run 2,500 network requests simultaneously from asyncIO and Python.
- 46:52At that point, you're probably saturating your network connection unless you're talking to mostly local hosts.
- 46:58At Facebook, when you're talking about a monitoring system, that's actually what you're doing is you're almost certainly talking to things that have super low latency to talk to and super high bandwidth.
- 47:07And so this was essentially the answer to that is like run asyncIO event, asyncIO event loops on a whole bunch of child processes, and then do a bunch of really like smart things to balance the load of the tasks that you're trying to run across all of those different processes in order to try and make them execute as quickly as possible.
- 47:29And then also, whenever possible, try to reduce the amount of times that you're serializing things back and forth.
- 47:35So one of the other common things that having more processes enables you to do is actually do some of the work to process, filter, aggregate that data in those child processes, rather than pickling all the data back to the parent process and then, you know, dealing with it and aggregating it there.
- 47:53Right, because you've already got the like scale out for CPU cores.
- 47:56Yeah, so it kind of gives like a local version of MapReduce, where essentially you're mapping work across all these child processes, and then inside each batch or whatever, you're aggregating that data into the result that you then send back up to the parent process, which can then process and aggregate that data further.
- 48:13Yeah, super cool. You gave a talk on this at PyCon in Cleveland, one of the last real actual in-person PyCons.
- 48:20Yeah, first one I've ever attended and the first one that I've ever given a talk at.
- 48:25Yeah, that was a good one, the one in Cleveland.
- 48:27Yeah, the room was absolutely massive and terrifying, and I don't know how I managed to do it all.
- 48:32Yeah, it's just kind of block it out, block it out. But no, it's all good. Cool, yeah, so I'll link to that as well. People can check that out. And it really focuses on this aiomultiprocess part, right?
- 48:41Yeah. Nice. All right, last of the AIO things at OmniLib is aioitertools.
- 48:48Yeah, so you kind of hinted on this before, like iter tools is mostly a bunch of helpers that let you process lists of things or iterables in nicer ways. And aioitertools is just basically taking the built in functions, like iterating, getting the next thing from an iterable or mapping, or chaining between multiple iterables or whatever.
- 49:11And essentially bringing that into an async first world. So all of the functions in aioitertools will accept both like normal standard iterators or lists or whatever, as well as async iterables or generators or whatever.
- 49:27And essentially, it up converts everything to an async iterable, and then gives you more async iterable interfaces to work on these.
- 49:36So I know how to create a generator with like yield. So I can have a function, it does a thing, and then it goes through some process and it says yield an item, like here's one of the things in the list.
- 49:46That's already really good because it does like lazy loading, but it doesn't scale the waiting time, right? It just waits.
- 49:52So for the async generator, what's the difference there?
- 49:55In this case,
- 49:56Never tried one of those.
- 49:57If you just call the function async def and then have a yield statement in it, it creates an async generator, which is just an async iterable object that similar to how when you call a coroutine, it's an object, but it doesn't actually run until you await it.
- 50:13With an async generator, calling it creates the generator object, but you don't actually...
- 50:18Then the async part is done, right? At that point.
- 50:20Well, it's like it doesn't, it still doesn't even start running it until you actually start to use the async for some other async iteration to then iterate over it.
- 50:30If you're using the async iterator, you still get the lazy loading of everything like with a normal generator, but you also have the potential for your thing to be interrupted.
- 50:38The common use case here or the expected use case would be if you're doing something like talking to a whole bunch of network hosts and you want to return the results as they come in,
- 50:50as an async iterable, then you could use something like aioitertools to then do things like batch up those results or run another coroutine across every result as it comes in, things like that.
- 51:02The other added benefit in here is that there's also a concurrency limited version of gather.
- 51:08So as I said earlier, when you have a whole bunch of tasks, you're actually making the event loop do a whole bunch more work.
- 51:13One of the common things I've seen is that people will spawn 5000 tasks and each task or they'll all have some semaphore that limits how many of them can execute at once.
- 51:24But you still have 5000 tasks that the event loop is trying to service.
- 51:28And so you're giving a whole bunch of overhead every time it wants to switch between things.
- 51:33It's got to potentially go through up to 5000 of them before it gets to one that it can actually service.
- 51:38So the concurrency limited version of gather that aioitertools has lets you specify some limit, like only run 64 things at a time.
- 51:47And so it will, you know, try to fetch the first 64 things of all of the coroutines or awaitables that you give it.
- 51:55And it will start to yield those values as they come in.
- 51:57But essentially, it's making sure that the event loop would never see more than 64 active tasks at a time, at least from that specific use of it.
- 52:05Yeah, yeah, they're just hanging out of memory.
- 52:07They don't really get thrown into the running task.
- 52:10So one of the challenges or criticisms almost I've seen around asyncio is that it doesn't allow for any back pressure or whatever, right?
- 52:20Like if I'm talking to a database, it used to be that the web front end would have like some kind of performance limit.
- 52:25It could only go so hard against the database.
- 52:27But if you do just await it, like all the traffic just piles in until it potentially can't take it anymore.
- 52:33And it sounds like this has some mechanisms to address that kind of generally speaking.
- 52:38That's at least the general intent of it is to be able to use this concurrency limit to try and prevent overloading either the event loop or your network or whatever.
- 52:47So even if you have 5000 items, by setting the limit to 64, you know that, you know, you're only going to be doing that many at a time.
- 52:55And then you can combine that that concurrency limited gather with something like the result of that is its own async iterable.
- 53:02And then you could also combine that with things like chain or other things in order to mix that in with the rest of the like iter tools functional lifestyle, if you will.
- 53:13Yeah, yeah, yeah.
- 53:14Super cool.
- 53:15I can imagine that these might find some way to work together.
- 53:18You might have some asyncio, aioitertools thing that then you feed off to aiomultiprocess or something like that.
- 53:26Do you put these together any?
- 53:28Yeah, exactly.
- 53:29These are definitely a whole bunch of tools that I've put together in various different use cases.
- 53:33Yeah, very neat.
- 53:34All right.
- 53:35Well, we're getting quite near the end of the show.
- 53:37I think we've talked about a lot about these very, very cool libraries.
- 53:41So before we get out of here, though, we touched on this at the beginning, but I'll ask you this as one of the two main questions at the end of the show.
- 53:47If you're going to write some Python code, what editor do you use?
- 53:50The snarky answer is anything with a Vim emulation mode.
- 53:53That was the thing that I learned in college.
- 53:55And I specifically avoided answering that earlier when we were talking about it.
- 54:01But that's what I learned when I was writing a whole bunch of PHP code.
- 54:04And that's what I used for years.
- 54:06And then eventually I found Sublime Text.
- 54:09And I really liked that.
- 54:10But it kind of seemed dead in the water.
- 54:12Adam came out, but Adam was slow.
- 54:15And so these days I'm using VS Code primarily because it has excellent Python integration, but also because it has a lot of like Facebook builds a lot of things that we used to have on top of Adam called Nuclide, which especially were a lot of like remote editing tools.
- 54:31Okay.
- 54:32We've rebuilt a lot of those on top of VS Code because VS Code is faster, nicer, you know, has better ongoing support from the community and so forth.
- 54:40Nice.
- 54:40Yeah.
- 54:40VS Code seems like the natural successor to Adam.
- 54:43Yeah.
- 54:43And like I said before, it's like I had tried PyCharm at one point, but it's one of those cases where I touch just enough stuff that's not Python that I really want my tools to work and function the same way regardless.
- 54:55And so VS Code has the better sort of like broader language support where it's like there's some days where I just have to write a bash script and I want it to be able to do nice things for bash or, you know, I use it as a markdown editor and it has a markdown preview.
- 55:09Things like that.
- 55:10Yep.
- 55:10All right.
- 55:11Cool.
- 55:11Sounds good.
- 55:11And then notable PyPI package.
- 55:13I mean, I guess we spent a lot of time on four of them, right?
- 55:15Yeah.
- 55:16I've also talked about microsort, usort.
- 55:18Yeah.
- 55:19So the joke answer is I have a package called aioseinfeld that's built on top of aiosqlite.
- 55:25And essentially you give it a database of Seinfeld scripts and you can search for things by actor or by keyword of what they're saying.
- 55:35And it will essentially give you back some elements of dialogue from a script that contains your search query.
- 55:42And this is powering a site I have called seinfeldquote.com, which is basically just a really old bootstrap template that lets you search for pieces of Seinfeld quotes.
- 55:53I also implemented a chat bot in Discord for some of my friends that also uses this.
- 55:59The more serious answer would be the other one that we didn't talk about from OmniLib, which is attribution, which is essentially a quick program to automate the generation of change logs and to automate the process of cutting a release for a project.
- 56:14And so I use this on all of the OmniLib projects.
- 56:16And essentially I type one command attribution release.
- 56:20I'm sorry, attribution tag and then a version number.
- 56:23And it will drop a dunder version.py in the project directory.
- 56:29It will create a get tag.
- 56:32It lets you then type in what you want the release notes to be.
- 56:35It's assuming, you know, a markdown format.
- 56:37And then once it's made that tag, then it regenerates the change log for that tag and retags it appropriately.
- 56:44And so you get this really nice thing where the actual tag of the project has both the updated change log and the appropriate version number file.
- 56:52So you only ever type the version in once.
- 56:55You only ever type the release notes in once.
- 56:57And it gives you, you know, as much help and automation around that as possible.
- 57:02Oh, yeah.
- 57:02Okay, very cool.
- 57:03That's a good one.
- 57:04All right.
- 57:04Final call to action.
- 57:05If people are excited about AsyncIO, maybe some of the stuff at OmniLib, they want to get started.
- 57:10What do you tell them?
- 57:11If they want to get started on the projects, going to omnilib.dev is the easiest way to find the ones that are currently hosted on the project.
- 57:19We're always welcoming of code review from the community.
- 57:22So even if you're, you know, not a maintainer, if you are interested in reviewing pull requests and giving feedback on things, always welcoming of that.
- 57:32There's never enough time in my own personal life to review everything or respond to everything.
- 57:36Otherwise, if there are things in these projects that you are interested in adding, like new features or fixing bugs or whatever, either open an issue or just create a pull request.
- 57:46And I am more than happy to engage in design decisions or discussions or whatever.
- 57:52Make sure that ideally, if you open an issue first, make sure you're not wasting your time on a pull request that's going in the wrong direction.
- 57:59Right, right.
- 57:59Because people might have this idea, but you're like, this is really inconsistent where this project is going to go or whatever.
- 58:05So even if it's perfect, you can't accept it, right?
- 58:08So good advice.
- 58:09If it's just like a bug fix or something, then, you know, probably just worth creating a pull request and I'm not going to bite your head off.
- 58:15But otherwise, the only other thing I would say is that LGBTQ things are very personal to me.
- 58:22And so I would ask that if you're in a position to do so, that you please donate to an LGBTQ charity that will help in the community.
- 58:30There's two that I really like.
- 58:32One is called Power On.
- 58:34And that's a charity that donates technology to LGBTQ youth.
- 58:39They're either homeless or disadvantaged.
- 58:41And they have that at poweronlgbtq.org.
- 58:45And then the other one is the Trevor Project, which is crisis intervention and a suicide hotline for LGBTQ youth.
- 58:52And that's at thetrevorproject.org.
- 58:54Yeah, awesome.
- 58:55Those are just two examples, but there are plenty.
- 58:57Worst case, just donate to a food bank near you.
- 58:59Cool.
- 59:00Yeah, that's a great advice.
- 59:01Great call to action.
- 59:02Seems like your projects are also really open to new contributors, people getting into open source.
- 59:06So participating in that way seems like a great thing.
- 59:10Fantastic.
- 59:10All right, John.
- 59:11Well, thank you so much for being on Talk Python.
- 59:13It's been great to have you here.
- 59:14Thank you for having me so much.
- 59:15I really appreciate it.
- 59:16This has been another episode of Talk Python To Me.
- 59:19Our guest on this episode was John Reese, and it's been brought to you by Linode and Talk Python Training.
- 59:25Simplify your infrastructure and cut your cloud bills in half with Linode's Linux virtual machines.
- 59:30Develop, deploy, and scale your modern applications faster and easier.
- 59:33Visit talkpython.fm/Linode and click the Create Free Account button to get started.
- 59:38Want to level up your Python?
- 59:40We have one of the largest catalogs of Python video courses over at Talk Python.
- 59:44Our content ranges from true beginners to deeply advanced topics like memory and async.
- 59:49And best of all, there's not a subscription in sight.
- 59:52Check it out for yourself at training.talkpython.fm.
- 59:54Be sure to subscribe to the show.
- 59:57Open your favorite podcast app and search for Python.
- 59:59We should be right at the top.
- 1:00:01You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:00:06and the direct RSS feed at /rss on talkpython.fm.
- 1:00:10We're live streaming most of our recordings these days.
- 1:00:14If you want to be part of the show and have your comments featured on the air,
- 1:00:17be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:00:21This is your host, Michael Kennedy.
- 1:00:23Thanks so much for listening.
- 1:00:24I really appreciate it.
- 1:00:26Now get out there and write some Python code.
- 1:00:28I'll see you next time.
- 1:00:48Thank you.