# Preventing \`yield\` inside certain context managers

**URL:** <https://discuss.python.org/t/preventing-yield-inside-certain-context-managers/1091>\
**Category:** Ideas\
**Created:** [April 2, 2019, 7:32am UTC](https://discuss.python.org/t/preventing-yield-inside-certain-context-managers/1091 "2019-04-02T07:32:12Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![njs](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/njs/32/204_2.png) [@njs](https://discuss.python.org/u/njs)\
**Post date:** [April 3, 2019, 8:37am UTC](https://discuss.python.org/t/preventing-yield-inside-certain-context-managers/1091/7 "2019-04-03T08:37:20Z")

</div>

> [@pf\_moore](#):
>
> Basically, I get from the original post that trio has a legitimate need for this feature, and how it might work at the implementation level. But I don’t get a sense of how to recognise _other_ cases where the feature would be useful, or how to use it in those cases.

I don’t know any other cases where this is useful either :-). Probably there are not many, since generators are [turning 18 next month](https://www.python.org/dev/peps/pep-0255/), and I haven’t heard anyone else complaining about this so far.

The [“structured concurrency” argument](https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/) 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](https://www.python.org/dev/peps/pep-0342/) 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.

> [@pf\_moore](#):
>
> don’t know how to link to individual messages in Discourse, sorry

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.)

---

_[View the full topic](https://discuss.python.org/t/preventing-yield-inside-certain-context-managers/1091)._
