(original) (raw)

On Mon, Oct 16, 2017 at 9:53 AM, Yury Selivanov <yselivanov.ml@gmail.com> wrote:
I think we can still implement context isolation in generators in
later versions for ContextVars. In 3.7, ContextVars will only support
async tasks and threads. Using them in generators will be
\*documented\* as unsafe, as the context will "leak out". Fixing
generators in some later version of Python will then be a feature/bug
fix. I expect almost no backwards compatibility issue, same as I
wouldn't expect them if we switched decimal to PEP 550 in 3.7.

Context also leaks into a generator. That's a feature too. Basically a generator does not have its own context; in that respect it's no different from a regular function call. The apparent difference is that it's possible to call next() on a generator object from different contexts (that's always been possible, in today's Python you can do this from multiple threads and there's even protection against re-entering a generator frame that's already active in another thread -- the GIL helps here of course).

I expect that any future (post-3.7) changes to how context work in generators will have to support this as the default behavior, and to get other behavior the generator will have to be marked or wrapped somehow.

--
--Guido van Rossum (python.org/\~guido)