[00:00]
I've done a couple of videos about
[00:01]
concurrency and Python's asyncio package
[00:04]
before asynchronous programming is
[00:06]
really useful especially if you interact
[00:08]
with apis and I wanted to Revis this
[00:11]
subject so I thought let's have a
[00:12]
slightly different take this time so
[00:14]
what I'm going to do in this video is
[00:16]
dive deeper into how asynchronous
[00:17]
programming actually works behind the
[00:19]
scenes using an event Loop talking about
[00:22]
events I recently was at Pyon Lithuania
[00:24]
which was a lot of fun it's a really
[00:26]
great meeting everyone one of you even
[00:28]
gave me a pair of lithu socks if you
[00:31]
were the person who gave them to me
[00:33]
thank you again they're very comfortable
[00:35]
actually I'm wearing them right now
[00:39]
look anyway if you want to learn how to
[00:42]
design a piece of software from scratch
[00:44]
while you're in excruciating pain check
[00:46]
out my free designu at iron. cdesign
[00:50]
guide this teaches you the seven steps I
[00:52]
take whenever I design a new piece of
[00:54]
software hopefully it helps you the link
[00:57]
is also in description of this video
[00:59]
ouch
[01:00]
and I'm feeling much better again it's
[01:03]
amazing asynchronous or concurrent
[01:05]
program means that you can run CPU bound
[01:08]
operations while you are waiting for iob
[01:11]
bound operations what are iio bound
[01:14]
operations well for example waiting to
[01:16]
read a file or waiting to get a response
[01:19]
from an API request or a database or
[01:22]
serving a web service so in short what
[01:24]
that means is that you can with
[01:26]
concurrent programming for example
[01:29]
launch a multiple AI calls concurrently
[01:31]
you don't have to wait for one to finish
[01:33]
in order to launch the next one or you
[01:35]
can do other things while you're reading
[01:36]
a file or if you building a server you
[01:38]
can serve multiple clients all at once
[01:41]
if you have that capability if you
[01:43]
integrate concurrent programming
[01:44]
properly into your software then your
[01:46]
software is also going to feel snappier
[01:49]
and I mean especially nowadays
[01:51]
applications interact with apis or
[01:53]
databases like all the time so doing
[01:56]
things concurrently can make a massive
[01:58]
difference in your user experience in
[02:00]
Python we have the async io library for
[02:02]
that and even in the latest python
[02:05]
versions 3.10 3.11 3.2 there's still
[02:08]
been updates to this Library so this is
[02:11]
changing and improving all the time now
[02:13]
concurrency is typically built with an
[02:15]
event Loop that manages asynchronous
[02:18]
tasks and this allows the execution of
[02:21]
multiple tasks seemingly in parallel but
[02:24]
actually this all happens within a
[02:26]
single thread take a look at this
[02:28]
diagram we have the script that creates
[02:30]
the event Loop and then adds tasks and
[02:32]
the event Loop then starts executing
[02:34]
these tasks so there is a loop that
[02:37]
basically goes over all of these tasks
[02:38]
and checks whether they have been
[02:40]
completed and if so that task is being
[02:42]
marked as completed so the script can
[02:45]
basically continue doing other things
[02:46]
while the event Loop takes care of the
[02:48]
tasks and then finally when all the
[02:50]
tasks are completed the event Loop is
[02:52]
closed so that's in principle how this
[02:54]
works in older versions of python I
[02:56]
think Python 3.4 and before you have to
[02:59]
actually do these things explicitly you
[03:01]
see an example of a script that shows
[03:03]
this so I'm importing the ASN KO package
[03:06]
which is used for concurrency and they
[03:08]
have a couple of functions do iio and do
[03:11]
other things now do iio does nothing
[03:13]
except caller sleep function but as you
[03:15]
can see in the main function I have to
[03:17]
actually get an event Loop and then call
[03:20]
run until complete on these functions
[03:22]
and then I have to explicitly close the
[03:24]
loop that's how you have to do things in
[03:26]
more recent versions of python this is
[03:28]
actually way simple you can just call
[03:30]
ASN ko. run and then you pass the
[03:32]
function that you want to run in this
[03:34]
case I'm calling that on the main
[03:35]
function which is a concurrent function
[03:38]
that's what the async word means in
[03:39]
front of the function signature now get
[03:42]
back to this example in a minute here I
[03:44]
have a very basic example of a server
[03:46]
that hosts a server on local host on
[03:49]
Port 3000 this server has very basic
[03:52]
implementation us a socket but most
[03:55]
importantly it runs synchronously and
[03:57]
that means that this server can serve
[03:59]
only a single single client at a time it
[04:01]
can serve client B if it hasn't finished
[04:04]
processing the request of client a and
[04:07]
that just means that this server is
[04:08]
going to be incredibly slow it's not
[04:10]
even that stable because I even think
[04:12]
it's quit with an error if a second
[04:14]
client tries to connect when the first
[04:16]
one is still being processed so it's not
[04:18]
really a great solution let me show you
[04:20]
what I mean so I'm starting the server
[04:21]
here and I have a little test script
[04:23]
here that calls the URL several times
[04:26]
concurrently so when I run this
[04:31]
you see that we get some sort of client
[04:32]
connection error and here actually Al
[04:34]
the server crashes now that might also
[04:36]
be for different reason I didn't really
[04:37]
do a very good job of building a very
[04:39]
stable server but handling client
[04:42]
request sequentially on a web server is
[04:44]
simply not a very good idea and this is
[04:46]
also one of the main reasons that
[04:48]
Frameworks such as fast API all use asyn
[04:51]
iio because that's just a much better
[04:53]
solution so here I have another version
[04:56]
of that same server except that now it's
[04:59]
asyn
[05:00]
and that's not just because we changed
[05:01]
the class name actually I'm using here a
[05:04]
syo and you can see that methods like
[05:06]
starting the server are asynchronous and
[05:08]
I'm using asyn kio's start server
[05:11]
function for that and that's typically
[05:13]
also how you can see easily that some
[05:15]
code is concurrent because it uses the
[05:18]
async and await keyword so async is to
[05:20]
indicate that something can run
[05:22]
concurrently and a wait means that
[05:24]
within that concurrent function method
[05:28]
we wait until a particular task is
[05:30]
completed before we continue with the
[05:32]
next one so here of course we want to
[05:35]
wait until the server has started to
[05:37]
actually uh get the socket and logging
[05:40]
that the server has started makes sense
[05:42]
right and then we call the serve forever
[05:44]
now what does this serve do actually not
[05:47]
even all that much it simply handles
[05:49]
basic requests and it serves an
[05:50]
index.html file which is simply a local
[05:53]
file that I have defined here and that
[05:56]
returns an image that's base 64 encoded
[05:59]
so let me start the asynchronous server
[06:02]
like so and then when I open the browser
[06:05]
we see that we get a beautiful image of
[06:07]
me and some sort of server help but most
[06:09]
importantly because this server cannot
[06:11]
handle requests concurrently our test
[06:14]
server script now runs without any
[06:16]
issue so let me try that again so we see
[06:20]
that it now serves these requests so
[06:22]
each takes about 2 seconds and that's
[06:24]
mainly because I added an ASN K sleep
[06:27]
here of 2 seconds so if I let's say um
[06:31]
put this in comments and then let me
[06:35]
restart the server so now the server is
[06:37]
restarted and when I run the test again
[06:40]
you see that the requests are now
[06:41]
handled really fast one thing in
[06:43]
particular that you notice in this
[06:44]
asynchronous version of this basic
[06:46]
server is that we also using some
[06:48]
asynchronous packages like IO files for
[06:51]
example so um I'm using that to in order
[06:55]
to generate response I'm using that to
[06:57]
open a file in this case that's just
[06:59]
than HTML file and honestly I wouldn't
[07:02]
recommend you implementing a server like
[07:04]
this yourself there's many great
[07:06]
Frameworks out there I've already
[07:07]
mentioned fast API but other examples of
[07:10]
great Frameworks to use are D Jango or
[07:12]
quartz now when you actually look at
[07:14]
this diagram that's sort of
[07:16]
representation of how request is handled
[07:19]
in our asynchronous server so you see we
[07:21]
have client that connects to the server
[07:23]
the server then accepts connections
[07:26]
that's handled by the request Handler
[07:28]
the request Handler has process request
[07:31]
and process request then handles the
[07:33]
request generates a response returns it
[07:35]
and we go back until we reach again the
[07:38]
client who then finally gets the
[07:39]
response and what asynchronous
[07:41]
programming allows us to do specifically
[07:43]
is that we can overlap this sequence of
[07:45]
function calls and that allows us to
[07:47]
perform the iob bound operations of
[07:49]
reading files and serving sockets while
[07:52]
we perform cpub Bond operations like
[07:55]
routing validation Etc and that's what
[07:57]
makes handling quests asynchronously a
[08:01]
way better solution than handling them
[08:03]
synchronously now you may wonder why do
[08:05]
we need async IO why not just use
[08:07]
threats well in Python specifically
[08:09]
threading has lots of limitations due to
[08:12]
the gild global interpreter lock and
[08:14]
that hinders true parallel execution
[08:17]
it's not the case of course for all the
[08:19]
languages but another thing is that
[08:21]
threads have a very different kind of
[08:23]
API than ASN the await and async
[08:27]
keywords are really helpful for handling
[08:29]
these types of concurrent requests if
[08:31]
you have to start a separate thread
[08:32]
yourself for each of these things that's
[08:34]
going to lead to a lot of boilerplate
[08:35]
code now async iio resolves this Global
[08:38]
interpreter lock problem in Python
[08:41]
because it enables concurrent execution
[08:43]
without using multi-threading and that
[08:45]
makes single threaded applications that
[08:48]
have IO bound tasks way more efficient
[08:51]
and like I mentioned this dramatically
[08:52]
increases performance if you're building
[08:54]
a web server if you need to read
[08:56]
multiple files if you need to interact
[08:58]
with apis and and so on and this all
[09:00]
works via this core event Loop combined
[09:03]
with other things such as cues now like
[09:05]
I mentioned in the asynchronous server
[09:06]
I'm using some libraries in particular
[09:08]
AIO files to read files asynchronously
[09:11]
and that's an issue that you're going to
[09:12]
encounter more often in Python
[09:15]
unfortunately most python packages don't
[09:17]
really have great support for
[09:20]
concurrency and that means that next to
[09:22]
the basic packages that are part of the
[09:24]
Python standard Library you're going to
[09:25]
have a bunch of libraries that provide
[09:28]
concurrent version of these things that
[09:30]
are then slightly different so the
[09:33]
python ecosystem at the moment in my
[09:35]
opinion doesn't really serve concurrent
[09:38]
programming all that well I would much
[09:40]
prefer if the standard libraries in
[09:41]
Python had better support for this but
[09:43]
unfortunately at the moment that's not
[09:45]
the case so we have this kind of
[09:46]
fragmented environment that we have to
[09:48]
deal with you see another example of
[09:50]
where that plays a role so let's say
[09:52]
you're building an API that interacts
[09:54]
with a sqlite database happens quite
[09:57]
often where you have some local uh
[09:59]
database that you need to interact with
[10:01]
and using SQL lights just I don't know
[10:03]
to store some settings or do whatever
[10:05]
you want to do but if you want to do
[10:07]
like currently you can't use the build
[10:08]
in sqlite you have to use another
[10:10]
package in this case I'm using iio
[10:12]
sqlite to do that and then when you have
[10:14]
that your API server can use ioq light
[10:18]
asynchronously to connect to let's say a
[10:21]
database and then execute some sort of
[10:23]
query right so what we've done in this
[10:25]
example you can by the way just get this
[10:27]
example by going through the examples
[10:29]
get repost if put the link in the
[10:31]
description of this video we've
[10:32]
basically implemented most of the crud
[10:35]
methods so there is getting books
[10:36]
getting movies adding books and movies
[10:39]
uh deleting them the basic things that
[10:41]
you would need in an API and when I
[10:42]
start the server I create the necessary
[10:44]
tables and what's interesting about this
[10:47]
piece of code is that I'm using the ASN
[10:49]
KO task group feature and this is
[10:53]
similar to ASN gather I'll show you an
[10:55]
example of that in a minute so it allows
[10:57]
you to start various tasks concurrently
[11:00]
but it has better functionality than
[11:01]
gather gather is simply to use but task
[11:04]
groups have much better functionality
[11:06]
allowing you to uh deal with cancel
[11:08]
tasks or uh you can use it together with
[11:11]
exception groups so that exceptions are
[11:13]
properly propagated properly propagated
[11:16]
properly propagated so you can handle
[11:19]
them uh as you see fit and with Galler
[11:22]
this just doesn't work all that well and
[11:24]
by the way if you like these types of
[11:25]
very technical discussions you might
[11:27]
also like my Discord server go to disc
[11:29]
IR codes to join there's lots of people
[11:32]
there very knowledgeable it's really
[11:33]
great community and I hope you also join
[11:36]
us now I'm going to start this API
[11:41]
server so again this use exactly the
[11:43]
same URL and Port just for testing right
[11:47]
but then let's also run
[11:50]
the async API ciance which is this file
[11:55]
that then calls a bunch of these API
[11:57]
requests concurrently in a batch and
[12:00]
this uses a. gather because we don't
[12:03]
really care about canceling task or
[12:05]
dealing with exceptions in this case so
[12:08]
started the server right here and now
[12:10]
I'm going to run this API client and you
[12:12]
see it adds these uh books and movies
[12:14]
and delete them again as we expect and
[12:17]
again because the standard request
[12:19]
function from urel doesn't support
[12:21]
concurrency we have to use a special
[12:23]
library for that I'm using ioh HTTP here
[12:26]
because all the libraries in this code
[12:28]
example sty with iio so why not but as
[12:31]
an alternative you can also use httpx
[12:34]
which also supports concurrent HTTP
[12:37]
request so now I'd like to hear from you
[12:39]
how often do you rely on concurrency to
[12:42]
make your code be more Snappy and are
[12:45]
there particular libraries that you like
[12:47]
to use do you agree that the python
[12:50]
standard Library should have better
[12:51]
support for concurrency or are you okay
[12:53]
with using these other libraries instead
[12:56]
let me know in the comments so in this
[12:58]
video we've talk about the difference
[13:00]
between synchronous and asynchronous
[13:03]
code I've talked about a few libraries
[13:05]
that I'm using to help support that and
[13:07]
showed you a couple of examples of how
[13:10]
you could incorporate this into your
[13:12]
apis I really encourage you to
[13:15]
experiment with this and see if there
[13:17]
are some areas in your application where
[13:19]
you can apply concurrent programming to
[13:21]
make your code more performant now if
[13:23]
you want to learn more about asq and get
[13:25]
some useful tips on how to get started
[13:27]
doing that watch this video next thanks
[13:30]
for watching and see you soon