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