I am not totally clear on how closures work in terms of thread-safety in Python, especially in regards to generators. For example, if I call a function from multiple threads (or multiple processes via multiprocessing) that looks something like this:
def count_to_ten():
def counter_gen():
for i in range(10):
yield i
return [i for i in counter_gen()]
Will the lists of numbers returned to my threads be [0, 1, 2, 3, ..., 9] or will there be race conditions, and I will end up with threads getting different results?
Clearly the following will not be thread safe:
def counter_gen():
for i in range(10):
yield i
But if it is wrapped up in a function and defined as a closure (like the first code snippet), is it thread-safe?