Async Techniques and Examples in Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Coordination with Trio
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The next extra library we looked at was Trio and Trio's main purpose is to take this async and await world
0:08
and put a little bit nicer coordination on it. So yes, it's easy to start some operation on an asyncio event loop but what about child tasks?
0:17
What about weight and making sure that the parent task doesn't complete until its child tasks do complete?
0:23
What if you have a whole bunch of work running and you get an error in one? Wouldn't you want to cancel the other work because you don't want to do it?
0:29
Everything's broken, anyway. So, that's the job of Trio, is that sort of coordination. So, here's a typical use case.
0:36
We're going to go and generate some data, and process data. We talked about this too You're probably tired of hearing about this
0:42
producer consumer thing but the idea is we're going to open a nursery nurseries where we start these child tasks and the async with block won't finish
0:51
until all the child tasks are done. One of them is errored, in which case we'll cancel all the others and it'll be done
0:57
or as you can see at the top here, we've added a move_on_after block as well so we're only going to allow that nursery to operate for five seconds.
1:06
If it's still busy, we're going to make it cancel all of its tasks. So, either all the tasks are done there's been errors, in which case
1:13
an exception will be raised right here or the work has been canceled. So, a really nice way to coordinate
1:19
parent child tasks, and timeouts, and things like that. Just remember, if you're using Trio it's separate and independent from asyncio
1:28
so things built on asyncio, like aiohttp require that Trio-async bridging the library that I talked about.