Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Concurrency Improvements
Lecture: Other asyncio Changes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, that was a lot of, asyncio there's a whole lot more in that great long change log that represents
0:06
the changes to 3.11. I pulled out a couple more that I think are interesting but not really
0:12
worth exploring with code itself. So let's see a few more first of all, github issue 97545 we have make semaphore run faster,
0:23
so semaphore's are a little little like barriers, like I'm going to only let a certain number of things
0:29
run, but they're kind of in reverse, like if you said to have a semaphore, five items, I want five or fewer to run,
0:36
whereas the barrier says we have to have all five run than it goes. So it's a coordination mechanism much like that, that is very,
0:42
very powerful and apparently faster in 3.11. Like many things, we also have a default number of workers for the thread pool,
0:52
executor now and that is five times the number of cpu's a lot of guidance says,
0:58
well the thread pool, the number of threads you should use optimally is exactly the number of processors in your
1:04
CPU or number cores in your CPU not necessarily true one, you might have hyper threading, so you might want to double that two.
1:12
If any of those threads are waiting on network stuff. So if a thread is talking to a database or an api or a file or many,
1:18
many other things, it's not consuming the CPU, in which case you unlock more work looks like,
1:23
that's what the python folks decided that five x is probably a good number.
1:29
There was a loop parameter as an async iO event loop that was passed to a asyncio sub process
1:36
and tasks function that's no longer needed awesome because it can just internally call,
1:41
get the running event loop. There's a way to mock various things in python.
1:47
There's that works on synchronous functions and classes but it doesn't work on async things.
1:53
So we have async mock but apparently async mock didn't correctly patch static or class methods that were async Now it does hooray some more BPO-39349.
2:10
We have a concurrent.futures.executor that we can run to coordinate different running tasks and you can call
2:17
shut down on it. That now takes a parameter called canceled futures which will look at all the things that
2:24
we're going to be run if there are any that are not yet started will be canceled previously. What happened is I said, well, you want me to shut down?
2:32
Well, I gotta wait for all the work that's running fine.
2:35
But also the work that's scheduled. So there's a possibility canceling the work now in 311 and this one caught
2:43
me out when I upgraded really quickly to Python 3.11 and the Mongo dB motor. The async Mongo DB library was using this the asyncio.coroutine used to
2:55
be the way that you would declare that a function is async by saying it's an at asyncio. coroutine of course. Now we just say async def.
3:04
Such and such. This older way with decorators is gone. It said it was deprecate in 38 and is scheduled to be removed,
3:13
was supposed to be removed in 310, but now it's really,
3:16
really gone and I know because my code broke for a day until the Mongo DB folks fixed their library. All right, well that's asyncio in Python 3.11.
3:25
I still have one more thing for you, but it falls a little more in the error handling than it does in the async side, but it could go either way.
3:34
So maybe this is all the awesome stuff for asyncio. or maybe we got a little bit more coming. Nonetheless a bunch of great additions.