generators shared among threads

Bryan Olson fakeaddress at
Sat Mar 11 02:15:20 CET 2006

jess.austin at wrote:
> You'll get the same result without the lock.  I'm not sure what this
> indicates.  It may show that the contention on the lock and the race
> condition on i aren't always problems.  It may show that generators, at
> least in CPython 2.4, provide thread safety for free.  It does seem to
> disprove my statement that, "the yield leaves the lock locked".
> More than that, I don't know.  When threading is involved, different
> runs of the same code can yield different results.  Can we be sure that
> each thread starts where the last one left off?  Why wouldn't a thread
> just start where it had left off before?  Of course, this case would
> have the potential for problems that Alex talked about earlier.  Why
> would a generator object be any more reentrant than a function object?
> Because it has a gi_frame attribute?  Would generators be thread-safe
> only in CPython?

I have not found definitive answers in the Python doc. Both
generators and threads keep their own line-of-control, and
how they interact is not clear.


More information about the Python-list mailing list