generators shared among threads

Tim Peters tim.peters at gmail.com
Mon Mar 13 03:40:19 EST 2006


[Paul Rubin]
> It looks to me like you can't have two threads in the same generator:

You can't even have one thread in a generator-iterator get away with
activating the generator-iterator while it's already active.  That's
an example in PEP 255:

"""
  Restriction:  A generator cannot be resumed while it is actively
    running:

        >>> def g():
        ...     i = me.next()
        ...     yield i
        >>> me = g()
        >>> me.next()
        Traceback (most recent call last):
         ...
          File "<string>", line 2, in g
        ValueError: generator already executing
"""

Same thing if more than one thread tries to do that, but perhaps
harder to see then.

To make some intuitive sense of those, note that a generator-iterator
reuses a single stack frame across resumptions.  There is only once
such frame per generator-iterator, hence only  (among other things)
one "program counter" per generator-iterator.  It should be obvious
that multiple threads can't be allowed to muck with _the_ frame's
program counter simultaneously, and the example above is actually
subtler on that count (because nothing in non-resumable functions
prepares you for that generators make it possible for a _single_
thread to _try_ to "be in two places at the same time" inside a single
invocation of a function -- although any attempt to try to do that
with a single thread is necessarily convoluted, like the example
above, the implementation still has to prevent it).



More information about the Python-list mailing list