generators shared among threads

jess.austin at jess.austin at
Sat Mar 4 19:45:12 CET 2006


This seems like a difficult question to answer through testing, so I'm
hoping that someone will just know...  Suppose I have the following
generator, g:

def f()
    i = 0
    while True:
        yield i
        i += 1

If I pass g around to various threads and I want them to always be
yielded a unique value, will I have a race condition?  That is, is it
possible that the cpython interpreter would interrupt one thread after
the increment and before the yield, and then resume another thread to
yield the first thread's value, or increment the stored i, or both,
before resuming the first thread?  If so, would I get different
behavior if I just set g like:


If both of these idioms will give me a race condition, how might I go
about preventing such?  I thought about using threading.Lock, but I'm
sure that I don't want to put a lock around the yield statement.


More information about the Python-list mailing list