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
0:03 and Trio's main purpose is to
0:05 take this async and await world
0:07 and put a little bit nicer coordination on it.
0:10 So yes, it's easy to start some operation
0:13 on an asyncio event loop
0:14 but what about child tasks?
0:16 What about weight and making sure that
0:18 the parent task doesn't complete until
0:20 its child tasks do complete?
0:22 What if you have a whole bunch of work running
0:24 and you get an error in one?
0:25 Wouldn't you want to cancel the other work
0:27 because you don't want to do it?
0:28 Everything's broken, anyway.
0:30 So, that's the job of Trio, is that sort of coordination.
0:33 So, here's a typical use case.
0:35 We're going to go and generate some data, and process data.
0:38 We talked about this too
0:40 You're probably tired of hearing about this
0:41 producer consumer thing
0:43 but the idea is we're going to open a nursery
0:45 nurseries where we start these child tasks
0:48 and the async with block won't finish
0:50 until all the child tasks are done.
0:53 One of them is errored, in which case
0:54 we'll cancel all the others and it'll be done
0:56 or as you can see at the top here, we've added
0:59 a move_on_after block as well
1:02 so we're only going to allow that nursery
1:04 to operate for five seconds.
1:05 If it's still busy, we're going to make it
1:07 cancel all of its tasks.
1:08 So, either all the tasks are done
1:10 there's been errors, in which case
1:12 an exception will be raised right here
1:14 or the work has been canceled.
1:16 So, a really nice way to coordinate
1:18 parent child tasks, and timeouts, and things like that.
1:22 Just remember, if you're using Trio
1:24 it's separate and independent from asyncio
1:27 so things built on asyncio, like aiohttp
1:30 require that Trio-async
1:32 bridging the library that I talked about.