MongoDB with Async Python Transcripts
Chapter: Welcome to the Course
Lecture: Python's asyncio
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Another important technology that we're going to focus on in this course is Python's asyncio.
0:07
When you see that async and await keywords in Python, that's what we're talking about.
0:12
Now, many of you may have preconceived notions about parallelism, about multiple cores and multi processing and threads and all those things.
0:22
And in Python, well, pretty much any of the asyncio frameworks, even outside of Python, they're a little bit different.
0:30
different. So for example, if you were to go create a thread in Python to try to do
0:35
concurrent work, first of all, you'd hit the gill, that's a bit of a problem. But it would
0:40
go and create an operating system thread that does something separate for that work. And
0:44
then you can sort of exchange data through shared variables. Multiprocessing is like
0:49
that as well, but it uses a separate process. But asyncio, it's a little bit different.
0:55
By default, in asyncio, there are no threads, there's one thread, the main thread that you're
0:59
working on, it does all that work, all that concurrent work on the same thread.
1:05
But it does it by saying, let's take instead of doing one big block of work, like let's
1:09
query the database and wait for MongoDB to respond, it says, let's begin querying the database.
1:16
And then we're also going to have another part of that work, which is receiving the response and then you'll maybe deserializing it and so on.
1:23
We might be doing other work at sort of the same time. So the idea is we're going to break up these jobs,
1:29
like let's say this green one is querying MongoDB. Instead of just one big long blocking bit, we're going to break it up into these pieces.
1:36
And where we're waiting, we can interleave other ones. So for example, we can have some task like talking over a network for an API call
1:46
might be happening here. This one here, this one's MongoDB. First we start the query, then we check on it,
1:51
then we get the response, and then we deserialize it, so on. So we're going to spend a whole chapter talking about how does async I work?
1:59
How do we do this with Python code and making sure your foundations for asyncio are really solid.
2:05
Now, a lot of times people say, concurrency is really hard. Don't do it. Parallelism is super hard. All you're going to do is make mistakes.
2:13
Don't do it. I don't really believe that it's not that hard. The code that you write is almost identical.
2:20
There's a few new keywords, but they're pretty straightforward to use.
2:23
you kind of got to get your mind into a different space, but programmatically it's not that
2:29
different, but there's a ton of advantages as we'll see throughout this course.
2:34
And what's really awesome is Beanie is going to allow us to do async I/O against MongoDB.