Transcript
This transcript was autogenerated. To make changes, submit a PR.
So I, the three Python concursive patterns
introduction, this is my name.
My name is Ian Oluwajao, I'm from Nigeria.
This is my Twitter account, my GitHub account, my technical blog, and
my AI related substat newsletter.
I'm a software engineer and AI engineer.
So what is concurrency?
Concurrency is when two or more tags run in an overlapping manner.
So I have an analogy to explain what concurrency means.
Imagine you have two YouTube, you have two You are playing two videos with YouTube.
So there is a YouTube video account 1 and the second video 2.
Now you are trying to play the two.
Now, but the difference is You play the first one After it has loaded to
our next point to a point and then the second one is post So the first one the
first video You are playing it because There's enough and it has loaded already
Then the second video you pause While it is loading basically, so basically
the way it works is There's a pause and play For it to overlap basically, so the
first video is paused While the second one is plain and then the second one
is paused while the first one is plain.
So that's concurrency So that's the analogy I used to explain concurrency now
for Parallelism is when two or more tags run at the same time So the analogy is
now you're playing the first video and the second video, the YouTube video, the
first YouTube video, and the second video.
Now you're playing simultaneously.
So there's no need for overlap.
So you're not pausing and playing.
So the two are playing simultaneously.
So that's parallelism.
So like I said, the first one, the first YouTube video, is playing while
the second YouTube video pauses.
The second YouTube video plays while the first YouTube video pauses.
So that's concurrency.
And then for parallelism is when the two YouTube videos are played simultaneously.
So that's parallelism.
So Now, this is my definition.
Now this, works because now for parallelism it works
because there is multiple CPUs.
So because there are multiple CPUs they can run simultaneously.
That's parallelism.
So that's when you can use parallelism.
Now for concurrency, it is one CPU, but there are tools.
in, in place for it to run.
So here you create where, explain more about how they work for Concur, basically.
So now when do you need Concur and when do you need parallelism?
So you need concurrency when.
It requires CPU bound problems.
So for example, mathematical problems.
So it makes sense.
That's when you need CPU and then you need multiple CPUs.
Now, for IO bound is when maybe most, when you need, concurrency,
when there are IO bound problems.
So maybe for example is a network call.
So you need.
Concurrency, so an example was the YouTube video Yeah, there are two YouTube
videos, of course, and then there is network call problem So because so the
first one plays while the second one is pausing No, the reason it is pausing
is because there's a it is waiting.
So it's buffering basically So it's pausing And then, the first
one you play, the second one you pause, or the first one you pause,
the second one you play, basically.
So that's when you need concurrency.
to be able to manage the two YouTube videos playing.
Now, so this is normally when you want to run a code for this
particular example I'm using.
It is synchronously.
So because it is synchronously, it will work one after the other.
So when the first one completes, then the second one too.
An example is, so because we have the two YouTube videos.
Now you need to complete the first video before you move to the second video.
So the first one, even while it's waiting, you have to wait and then it pauses.
It continues basically.
So then when you finish the first video, then you move to the next video.
So that's synchronously basically.
And then in that scenario, you use the Python request library.
So the Python request library is what you use, basically.
So it processes on its own.
It plays on its own.
And you just continue until the total number of tags has performed, basically.
So in this scenario, it is, you have two YouTube videos.
Now.
You need to, so after the first video, it is out on the, after you finish the first
video, then you move to the next video.
So it is one after the other, basically.
you use the request library.
Now, when do we need trading?
Now, this is the code for trading to explain how trading works in Python.
we use the trading method.
library in the standard library in Python.
So a thread is a different flow of execution.
The threads are not executed at the same time.
There are two major differences from the first program.
in that now, we now have a thread pool executor.
So this creates a thread, a pool of threads that run concurrently and an
executor that manages how the threads run.
So in this scenario about the YouTube example.
each YouTube video is a thread, and then myself, I'm the executor, so I
decide when it pauses and when it plays, and it pauses and plays when there
is network, when there is buffering.
it pauses or it plays, depending on the network happening, basically, but
the threads are the two YouTube videos.
Now, the disadvantage of it is that it is the operating system that determines
when a tax is passed and another is paid.
data shared between trades must be protected.
now, for the example, the analogy I used, which is the YouTube video.
So the operating system determined when the tax is forced and the
operating system in this scenario now.
and then I decide when it's forced and when it's split now, and if there
is the need to get to share data between the two trades, it's risky.
So you need to protect the trade so that it becomes trade safe.
Now, the next is concurrency is asyncIO.
So like I mentioned, there is concurrency and then there is parallelism.
Now for concurrency, there are two libraries that you can use
to perform a concurrent stack.
So it's the first, like I mentioned, was shredding.
Now the second is now concurrency and is now asyncIO.
So this is the asyncIO library.
In the standard library, and then it has a specific keyword for async.
io, which is the async.
And then you can see the async here, and then you can see the
await keyword in the async.
io.
Now, so now the different, now one major difference between async.
io and trading is that you need the specific keywords,
which is async and await.
That is why in that scenario, we don't use requests.
We use async.
I-A-I-O-H-T-T-P.
So the reason is because request doesn't have async awai and
we've been waiting for long.
So for now we just use A-I-O-H-T-T-P,
so that's for trading and async io and concurrency.
Basically.
Now the next, we now go to.
Parallelism.
So here we use the music multi processing standard library in
the standard library basically.
So can you see this is the code.
This is an example of running multi processing.
So now in this scenario, it creates and can determine the
number of pools to create.
So in that same scenario, but now in this scenario, it is based on the
number of CPUs that you have available.
Now by default it creates the equivalent of the number of CPUs in your computer So
it is by default because it is based on the number of CPUs on your computer So now
you might not ask What's the difference between threading and asyncio because
initially we've already differentiated the different we have separated the difference
between The concurrency and parallelism.
So where the parallelism is for multiple processing and concurrency is for
trading and async io now for concurrency.
Now, how do you differentiate between trading and Async io?
I Or when to use threading or async type, most of them, it just makes
sense to use, okay, it depends.
It depends.
So if the, if there are libraries that implemented async
await, it makes sense to use.
The reason is because async.
io is faster.
Not just that, it is less resourceful compared with trading.
And that is it basically.
So the benefit is faster.
But the problem is that you need to find libraries that have implemented async.
io.
So is why when we use the A IOH TT P Library, we needed to use
A-I-O-H-T-T-P for Async to work.
If it doesn't, it'll fail.
Now when they are now in, in a scenario, they, a scenario like
bottle three in as from AWS, now we, they implemented a weight.
That's when you might now need to use trading too.
The first time to use if you make a decision on when to use it The first is to
ask is there is a library for async await In that particular tags if there is just
use async io Now if there are no libraries for that then just use threading basically
So now this is that's one of the problems with thread 2 is expensive it requires
ram memory it makes sense to just start with asyncio if there are libraries
that have implemented asyncio already.
If not, then you use threading.
what are the differences between the multiprocessing asyncio or threading?
So for concurrency tax or IO bound tax, you use async io and trading.
For CPU tax Bound tax, you use multi processing.
So that's how you differentiate when to use multiple process,
multiple processing, or when to use.
thank you for this talk, have a blessed day.