- Podcasts
- Python Bytes
- #197 Structured concurrency in Python
About this episodeTopics covered in this episode: Structured concurrency in Python with AnyIO The Consortium for Python Data API Standards Ask for…
- Structured concurrency in Python with AnyIO
- The Consortium for Python Data API Standards
- Ask for Forgiveness or Look Before You Leap?
- myrepos
- A deep dive into the official Docker image for Python
- “Only in a Pandemic” section
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#197 Structured concurrency in Python
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 197, recorded August 26th, 2020. Brian, can you believe it's the end of August? Even if I can't say it, it still is true? No, I can't. I don't know where August went. I just don't.
I thought this whole pandemic thing would make the summer seem long and slow. It seems like it just went faster. Yeah, I've got like a Lego kit that I was planning on doing like the first week of summer vacation, and it's still sitting here. So yeah, for sure. Yeah, there's a lot of things I want to get done before the sun goes away and rain starts for six months straight. That's a Pacific Northwest problem, but it's our problem. All right. Now this episode is brought to you by us as well. We'll tell you more about the things that we're doing that we think you will appreciate later. Right now, I want to talk about something that I think we might've covered before, but I don't know if we've ever satisfactorily covered it. Maybe this time we'll get a little closer and that's AsyncIO.
Oh yeah, I think that's a new topic. It's a totally new topic. Covered only less than GUIs. No. So there's a new, how should I put it, a new compatibility-like layer library that allows you to work a little bit better with AsyncIO and some of the other Async libraries that are not directly immediately the same as or built right on top of AsyncIO.
True. But where's Unsync, people? Unsync is the best of all four of those. I don't know where Unsync is. Anyway, Unsync is not part of this conversation, but Unsync plays a role a little bit like this thing I'm going to mention today is AnyIO. And it's a pretty clever name because the idea is that it provides structured concurrency primitives built on top of AsyncIO.
Well, that probably means your program exited while it was halfway done or something like that. Right? Or your thing returned a value before it waited for it to finish. Right? And at the low level, something that's a little bit frustrating or annoying that you've got to deal with is that you've got to make sure that all the stuff you started on the Async event loop, that you wait for that event loop to finish before your program completely shuts down or completely carries on.
And so that's basically the idea of this library. It's a compatibility layer across those three types, those three different well-known concurrency libraries that provides this structured concurrency. So you look at Wikipedia, they say structured concurrency is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by using a structured approach to concurrent programming.
So let's create a width block, do all the work in there. And then by the way, when you leave the width block, it's going to have made sure all the tasks that were started and the tasks started by those tasks and so on all finished. Oh, that's nice. Yeah, that's pretty cool. So the way it works is you basically go anyio.createTaskGroup and then from the task group, you can spawn other subtasks and it will keep track of those.
That's pretty cool, right? Yeah. Yeah. So I think that that's pretty neat. Also has other primitives. So that's like a real simple example. Other example or other things it does include synchronization, primitives, locks. So if you create a reentrant lock in Python, often called a critical section and things like C++ and whatnot, it's never, ever going to help you.
Well, maybe that's a little bit strong. It's likely not going to help you because those mechanisms come from the operating system process level. And what they do is they make sure two threads don't run at the same time. Well, with asyncio, it's all a bunch of stuff that's being broken apart on a single thread, right?
It's all on the one, wherever the event loop.run is, run to complete or whatever, like wherever that's happening, that's the thread. So like the thread locks don't matter. It's all the same thread. Like you're not going to block anything. So having primitives that will kind of function like threads to protect data while stuff is happening, while it's in temporarily invalid states, that's pretty cool for asyncio.
Like I'm going to kick off this thing. And when it comes back, I'm going to take the data and do something. But if you're modifying shared data structures, you could still end up in some kind of event loop, erase condition. It's not as bad as like true threading because you're not going to, it's, I don't believe it's like a plus equals, right?
And some other one is like reading in some kind of loop, like that level of higher order, like temporarily invalid state. That could be a problem for asyncio and you want some kind of lock. So this comes with that, it comes with streams, which are similar to queues, timeouts through things like move on after or fail after a certain amount of time and so on.
So it's pretty cool little library. Yeah, that's nice. Nice. My vote still for unsync is the best of the four, even though it was unmentioned. Isn't unsync built on those also? It's a compatibility layer that takes asyncio, threading, and multiprocessing and turns them all into things that you can await.
Oh, yeah. Yeah. So don't you think there should be like a standard, like a, they should get together like some consortium and have a standard about this? Yeah. Well, they probably should, but we're still in the early stages of figuring out what the right API is. That's right. That's why they haven't done it.
There's something else that has, that could use some standards and that's in a lot of data science libraries. There's an announcement that there's a new consortium for Python data API standards. So there is one happening and it's happening actually quite fast. They're getting started right away and there's activities to the announcements right away.
Then in September, I believe they're going to kick off some work on data frames or on, no, starting with arrays and then move on to data frames. And so, okay, I'm getting ahead of myself. There are little blurbs says, one of the unintended consequences of the advances in multiple frameworks for data science, machine learning, deep learning, and numerical computing is that there is fragmentation.
And in using the tools and then there are differences in common function signatures. They have one example that shows what the, generally a mean function to get the average or mean, people are going to like flame me for calling average mean, but as a commoner, I kind of think of those the same thing.
But anyway, they show eight different, frameworks then, and some of them are common with other ones. And so there's five different interfaces for over the eight frameworks for just the mean function for an array. Yeah. And what's crazy is like, they all are basically the same. They're so, so similar, but they're not the same, not code wise the same, but they might as well be.
Yeah. And so one of the issues is there's people are using more than one framework for different parts of their, maybe different parts of their data flow. And sometimes you can kind of forget which one you're using and having a lot of these things common actually would just make life easier, I think.
So I think, I don't know how far they'll get with this, but I think it's a really, so they're not trying to make all of these, these frameworks look exactly the same, but with, commonalities in arrays and data frames. Or, and they note that arrays are also called tensors. So those are, trying to make some of those common is, I think a really good idea for some of the easy, simple stuff.
why not? It seems like a great idea. It seems like a huge challenge though. Like who's going to give, whose function is going to be the one that's like, yeah, we're dropping this part of our API to make it look like everyone else's. Right. And that's why I think that they've, they've went through a lot of thought on how to go about with this process and try to convince people.
So they're working with, they're trying to kind of be in between the framework authors and maintainers and the community and try to do some, some review process for different APIs, put a proposal out, have feedback from both from, from the different projects and from the community to have, have more of a, you know, more input to try to make it.
It isn't just like one set of people saying, Hey, I think this should be this way. Yeah, no, it's, it's a good idea. It would be great if a lot of these applications or these frameworks may be renamed. If it's the same function, if it's like, for instance, mean in this example, if it's spelled exactly the same, maybe it should be the same API.
And if you want a special version of it, maybe have a, have a underscore with an extra, you know, some reason why it's different. you can have extra different functions. Yeah. It seems like you could find some pretty good common ground here. It's a good idea. And if they make it happen, you know, it'd just be easier to mix and match frameworks and use the best or different situations.
Now it runs there. Yep. I don't know if it's ever really going to be like, you can just swap out a different framework, but for some of the common stuff, it'd really be great. And that's why one of the reasons why we're bringing it up is so that people can get on board and start being part of this review process if they care about it.
Yeah. It also seems like there might be some room for like adaptive layers, like from CuPy import pandas layer or something like that, where it basically, you talk to the, in terms of say a pandas API and it converts it to its internal. It's like, Oh, these, these arguments are switched in order or this keyword is named differently or whatever.
Now, also good is the things that we're working on. Brian, you want to tell folks about our Patreon? Actually, we've kind of silently announced it a while ago, but we've got 47 patrons now and it's set up for a monthly contribution and we, everything really appreciate people helping out because there are some expenses with the show.
Okay. We'll double check. It can for sure. If it doesn't already. And also, I want to just tell folks about a couple of things going on over at Talk Python training. We're doing a webcast on helping people move from using Excel for all their data analysis to pandas, basically moving from Excel to the Python data science stack, which has all sorts of cool benefits and really neat things you can do there.
Very exciting. So this next one I want to talk about has to do with manners. What kind of developer are you? Are you a polite developer? You're talking to the framework. Are you always checking in with it to see how it feels, what you're allowed to do? Are you kind of a rebel? You're just going to do what you like.
That's one. And the other one is look before you leap or please may I. All right. And with the look before you leap, it's a lot of checks, like something you might do in C code. So you would say, I'm going to create a file. Oh, does the folder exist? If the folder doesn't exist, I'm going to need to create the folder.
And then I can put the file there. Do I have permission to write the file? Yes. Okay. Then I'll go ahead and write the file. Right. You're always checking if I can do this, if this is in the right state and so on. That's the look before you leap style. The ask for forgiveness style is just try with open this thing.
It's going to raise an exception. It's not going to return it like a value, like malformed, constant, weird thing. It's just going to throw an exception and say, you know, invalid thing on line seven or whatever. Right. And so what that means is even if you wanted to do the look before you leap, you probably can't test everything and you're going to end up in a situation where you're still going to have to have the try except block anyway.
Oh yeah. It's on Sebastian. Widooski. So yeah, it's his, I didn't realize that it was his article. So it's, his article. Anyway, he talks about like, what is the relative performance of these things and tries to talk about it from a, well, sure. It's cool to think of how it looks in code, but is one faster or one slower than the other?
Okay. And this actually came up on talk Python as well. And so I said, look, if we're going to come up with an example, let's have a class and a base class. And let's have the base class define an attribute. And sometimes let's try to access the attribute. And when you don't have the base class, it'll, or when you only have the base class, it'll crash, right?
Cause it's in the derived class. So let's say we have two ways to test. We could either ask, does it have the attribute and then try to access it? Or we could just right to access it. And it says, well, look, if it, if it works all the time and you're not actually getting errors and you're doing this, it's 30% slower to do the look before you leap.
That's a lot slower. Oh really? Because the raising of the exception, figuring out this call stack, all that kind of stuff is expensive. So instead of just going, does it have the attribute? You're going, well, let's do the whole call stack thing, every error, right? And create an object and throw it and all that kind of stuff.
His example doesn't have any activity in the ask for forgiveness. If it isn't there. That's the way I saw when I first read it as well. There's two sections. There's like one part where he says, let's do it with the attribute on the drive class and let's do it again a second time by taking away the attribute and seeing what it's like.
Like it's just basically a try except pass. Yeah. So what do you think about this? So what I think is you're going to have to write the try except anyway, almost all the time. And you don't want both. Like that doesn't seem good. That seems like just extra complexity. So when it makes sense, just go with ask for forgiveness.
Like I'm going to read this number I got off by off of the internet by web scraping and there's a million records here. I'm going to parse it. If you want to do that a lot, a lot faster, that might make a lot of sense. I actually have a gist example that I put up trying to compare the speed of these things in a mixed case.
So like the cases we're looking at here are kind of strange because it's like, well, there's, it's all errors or it's zero errors. Right. And then it doesn't really do anything, which are both weird. So I have this one where it comes up with like a million records strings. And most of the time they're number, they're legitimate numbers, like 4.2 as a string.
So I write a lot of tests around things. Yeah. And so either case checking for things or like, for instance, if it is, if it is input, if I've got user input, I'm checking for things. Yeah. I'm going to do it checks ahead of time because I want, because the behavior of what happens when it isn't there or when there's a problem, it isn't really a problem.
The one time that I'll do the test, the look before you leave style, is if I think I can fix it, right? Does this directory not exist? I'm going to write a file to it. Well, I'm just going to make the directory. Then I'm going to write to it, you know? Those kinds of tests can get you out of trouble.
Get in your repository. Make sure it's all up to date, right? Oh, I was wondering how you were going to do that transition. So, yeah. That's good. I was following a discussion on Twitter, and I think, actually, I think Anthony Shaw may have started it, but I can't remember. But dealing with different, if you've got a lot of repositories, just sometimes you have a lot of maintenance to do or a little, you know, some common things you're doing for a whole bunch of repos.
And there's lots of different reasons why that might be the case or related tools or maybe just your work. You've got a lot of repos. But there's a project that came up in this discussion that I hadn't really played with before, and it's a project called My Repos. And on the site, it says you've got a lot of version control repositories.
Sometimes you want to update them all at once or push out all your local changes. You may use special command lines in some repos to implement specific workflows. Well, the My Repos project provides an MR command, which is a tool to manage all your version control repositories. And the way it works is it's on directory structures.
So it's a, and I usually have all of my repos that I'm working with under a common, like, projects directory or something so that I know where to look. And so I'm already set up for something like this might work. And you go into, into one of your repos and you type, if you have this installed, you type MR register.
And it registers this under, registers that repo for common commands. And then whether you're in a parent directory or one of the specific directories and type a command, like for instance, if you say MR status, it'll do status on all of the repos that you care about or update or diff or something like that.
I like the idea a lot. So basically I'm the same as you. I've got a directory, maybe a couple of levels, but all of my GitHub repos go in there, right? I grouped them by like personal stuff or work stuff. But other than that, they're just all next to each other. And this would just let you say, go do a Git poll on all of them.
That's great. Yeah. Or like, for instance, at work, I've got often like three or four different related repos that if I switch to another project that I'm working on, I need to go through and make sure I'm not sure what branch I'm using or if everything's up to date. So being able to just go through all, like even two or three, being able to go and update them all at once or just even check the status of all, it'll save time.
And then for the show, at least somebody that interviewed for a testing code, at least, Adam Johnson wrote an article called maintaining multiple Python projects with my repos. And we'll link to his article in the show notes. Yeah. Perfect. I like this idea enough that I wrote something like that already.
You did. Well, what I wrote is something that will, it'll go and actually synchronize my GitHub account with a folder structure on my computer. So I'll go and just say like repo sync or whatever I called it. And it'll use the GitHub API to go and figure out all the repos that I've cloned or created and the different organizations like talk Python organization versus my personal one.
So it looks pretty nice. The one thing it doesn't do is it doesn't look like it doesn't go to GitHub and say, oh, what other repos have you created that you maybe don't have here? Maybe you want that. Maybe you don't. If you've like forked Windows source code and it's like 50 gigs, you don't want this tool that I'm talking about.
But if you have reasonable size things like I forked Linux. Okay, great. That's going to take a while. But normally it would be I think it would be pretty neat. Yeah. Another thing that's neat around managing these types of things is Docker. And did you know that Python has an official Docker image?
Right. So it's not super surprising. It's just called Python. Right. But it's yeah, it's just called Python. That's it. I believe so. Pretty straightforward working with it. But I'm going to talk about like basically looking through that Docker, that official Docker image. So Itamar Turner Trouring, who was on Talk Python not long ago, talking about Phil.
And we also talked about Phil and Python bytes, the data science focused memory tool. He wrote an article called a deep dive into the official Docker image for Python. So basically it's like, well, if there's an official Docker image for Python, what is it? How do you set it up? Because understanding how it's set up is basically how do you take a machine that has no Python whatsoever and configure it in a Python way?
So this is using Debian. That's just what it's based on. And it's using the Buster version because apparently Debian names all their releases after characters from Toy Story. I didn't know that. But yep. Buster. Buster is the current one. So it's going to create a Docker image. You create the Docker file.
You say this Docker image is based on some other foundational one. So Debian Buster. And then it sets up slash user slash local slash bin for the environmental path. Because that is the first thing in the path. Because that's where it's going to put Python. It sets the locale explicitly to the EMV language is to UTF-8.
So you can ask, hey, what version is in this system without actually touching Python? That's cool. And then it has to do a few things like register the CA certificates. Like, I've had people sending me messages that are taking courses. And they're trying to run the code from, you know, something that talks to requests, whether it's SSL certificate endpoint, HTTPS endpoint.
And they'll say, this thing says the certificate is invalid. I'm like, the certificate's not invalid. What's going on here? Right? And almost always, something about the way that Python got set up on their machine didn't run the create certificate command. So there's like this step where Python will go download all the major certificate authorities and like trust them in the system.
So that happens next. And then it actually will set up things like GCC and whatnot. So it can compile it. It's interesting. Downloads the source code, compiles it. But then what's interesting is it uninstalls the compiler tools. It's like, okay, we're going to download Python and we're going to compile it.
But you didn't explicitly ask for GCC. We just needed it. So those are gone. Right? Cleans up the PYC files and all those kinds of things. And then it gives an alias to say that Python 3 is the same as Python. Like the command, you could do it without the 3. Another thing that we've gone on about that's annoying is like, I created a virtual environment.
Oh, it has the wrong version of pip. Is my pip out of date? Your pip's probably out of date. Everyone's pip is out of date. Unless you're like a rare, like two-week window where Python has been released at the same time like the modern pip has been released. So guess what? They upgrade pip to the new version, which is cool.
And then finally it sets the entry point of the Docker container, which is the default command to do if you just say Docker run this image, like Docker run Python 3.8-slim-buster. If you just say that by itself, what program is going to run? Because the way it works is it basically starts Linux and then runs one program.
And that program exits, the Docker container goes away. And so it sets that to be the Python 3 command. So basically, if you Docker run the Python Docker image, you're going to get just the REPL. Interesting. Yeah, you can always run it with different endpoints like Bash and then go in and like do stuff to it or run it with MicroWSGI or Nginx or whatever.
But if you don't, you're just going to get Python 3 REPL. Anyway, that's the way the official Python Docker image configures itself from a bare Debian buster over to Python 3. Neat. Yeah, neat. I thought it might be worth just thinking about like, what are all the steps? And you know, how does that happen on your computer?
No, sorry. Nanner Nest. It's an optimal peanut butter and banana sandwich placement. So this is kind of an awesome article by Ethan Rosenthal. Talks about during the pandemic, he's been sort of having trouble doing anything. And so he really liked peanut butter and banana sandwiches when he was just still even.
So you take a picture that includes both the bread and the bananas or the banana you have. And it will come up with the optimal slicing and placement of the banana for your banana sandwich. Wow, this is like a banana maximization optimization problem. So if you want, you got to see the pictures to get this.
So like, if you're going to cut your banana into slices, and obviously the radius of the banana slice varies at where you cut it in the banana, right? Is it near the top? Is it in the middle? It's going to result in different size slices. On where do you place your bread, the banana circles on your bread to have maximum surface area of bananas relative to the what's left of the bread, right?
Something like that? Yes. And he's trying to maximize, make it so that you have almost all of the bites of the sandwich have an equal ratio of banana, peanut butter, and bread. Oh yeah, okay. It's all about the flavor. I didn't understand like the real motivation, but yeah, you want to have an equal layer, right?
So you don't want that spot where you just get bread. You actually learn quite a bit about all these different processes and there's quite a bit of math here talking about coming up with arcs for, you have to estimate the banana shape as part of an ellipse and using the radius of that to determine banana slices and estimates for, because you're looking at a banana sideways, you have to estimate what the shape of the banana circle will be and it's not really a circle, it's more of an ellipse also.
Yeah, there's a lot going on here. Some advanced stuff to deliver your bananas perfectly. I love it. Actually, this is really interesting. This is cool. And it's, I mean, it's a silly application, but it's also a neat example. Yeah, actually, and this would be, I think, a cool thing for, to talk about difficult problems and packing for like a teaching, like in a school setting.
I think this would be a great example to talk about some of these different complex problems. Yeah, totally. Well, that's it for our main items. For the extras, I just wanted to say I'll put the links for the Excel to Python webcast and the memory measurement course down there and we'll put the Patreon link as well.
The title is Job Requirements. This comes to us from Eduardo Orochena. Thanks for that. And the first scenario is the job interview where you're getting hired. And then there's the reality, which is later, which is the actual on the job day to day. So on the job interview, I come in, I'm an applicant here and Brian, the boss says.
