Preventing `yield` inside certain context managers

I don’t know any other cases where this is useful either :-). Probably there are not many, since generators are turning 18 next month, and I haven’t heard anyone else complaining about this so far.

The “structured concurrency” argument is that just like for and while loops are the right primitives for expressing looping control flow, and if/elif/else is the right primitive for expressing alternating control flow, nurseries are the right primitive for expressing concurrent control flow. Adding a new control flow primitive is a pretty rare thing, and generator control flow is itself pretty unusual, so it’s not too surprising that there’s some funky interactions. (Heck, when generators were initially added, yield was always forbidden inside try/finally, and with is just sugar for try/finally. It took 4 years to figure out the details to make them work together at all…)

I do want to emphasize though that nurseries aren’t unique to trio – they’re a generic tool for structuring concurrent programs. Asyncio wants to add nurseries too. And if for some reason you want to avoid all this async stuff and use threads instead, then implementing a threads-based nursery is probably a good way to do that.

FYI, the little timestamp in the upper-right-corner of each post is a permalink to that post specifically. (Also as you scroll up and down, the URL in your URL bar automatically changes to match the post you’re looking at, though I find it more cute than useful.)