Async Techniques and Examples in Python Transcripts
Chapter: async and await with asyncio
Lecture: Other async-enabled libraries

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's critical that the libraries we're using to talk to the various external systems support asyncio if we want to take advantage of them at all
0:11 in some sort of asyncio system. We might be able to use them in threading or other situations if they don't but to use them with asyncio, they need
0:20 to have asynchronous methods that we can call. So to give you a sense of how this works and how you find these libraries and so on
0:26 we're going to go through four libraries that talk to common, mainstream, external systems MongoDB, Postgres, file systems, and Redis.
0:34 Of course, you may want to talk to something else and that's totally fine. This is more to inspire you to say
0:39 there's probably some kind of asyncio-enabled library for the thing that you're working with. Now, if there's not, we'll talk later about
0:47 how to mix and match these things when we get to unsync and other stuff much further in the course. But, for now, we're going to just talk
0:54 about these libraries that enable asyncio for these four systems. Now, we're not going to do any demos here
1:00 so I'm just going to show you the github repos. So the first one, file IO. If I say with open, that's the standard way
1:08 to open a text file or a binary file in Python. I might also do like a JSON read when I give it like the filestream that came with open.
1:17 None of that stuff supports asyncio. It's ironic, right? We're doing file IO in Python that does not support asynchronous IO.
1:25 Anyway, that's a whole side discussion. So here is one project that you could use aiofiles. So it basically just has the ability to open files
1:35 and read and write from them asynchronously. And these are async methods you call and you can await them and use them properly
1:42 within your async methods. This is probably not the only one for Python but it's an example. If you need it, go have a look.
1:49 MongoDB, very popular NoSQL database. Its primary way to talk to it from MongoDB doesn't support asyncio, meaning the popular ODMs
1:59 Object Document Mappers don't support asyncio. However, here's a pretty cool one called umongo. umongo, it's supposed to be a mu, a Greek mu
2:09 at the beginning, even though it's a u. So umongo has both synchronous and asynchronous support for MongoDB. It's really cool, it actually has a bunch
2:19 of different subsystems you can swap in and out like Twisted versus asyncio, but not getting into that here is cool library that you can use
2:27 if you want to talk to MongoDB map classes to and from it, and you want to do that using async and await. Don't like MongoDB?
2:35 Postgres is probably the other most popular database choice for Python developers, and if you want to talk to it asynchronously you need asyncpg
2:44 not the standard one that doesn't support asyncio. You can check out asyncpg. It's very popular, and of course, lets you use Postgres
2:52 in async and await. Finally, our final example here is going to be Redis. If you want to talk to Redis, this is primarily used
3:00 as an in-memory cache, but also can be used for, like, queuing systems and things like that. Redis is quite popular in the Python space.
3:07 If you want to read and write objects from it connect to it asynchronously, well, asyncio-redis is one of the ways you can do that.
3:15 So like I said, this is not an exhaustive list by any means. It's just an example, a tour to inspire you that hey, if there's a thing I'm working with
3:24 there's a good chance that somewhere out there there's an asyncio library for it. And if there is, that's awesome, because
3:30 then you can go and plug that into your code and you've seen how much scalability and parallelism you can get by doing stuff
3:36 while you're waiting on external systems like Redis, or MongoDB, or even the file system.


Talk Python's Mastodon Michael Kennedy's Mastodon