generators shared among threads

jess.austin at gmail.com jess.austin at gmail.com
Thu Mar 9 05:41:57 EST 2006


Bryan,

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 started the discussion with simpler versions of these same questions.
 I'm convinced that using Queue is safe, but now I'm not convinced that
just using a generator is not safe.

cheers,
Jess




More information about the Python-list mailing list