Are range iterators thread safe?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Oct 20 04:04:16 EDT 2011


Using Python 3, are range_iterator objects thread-safe? 

I have tried this, and it seems to be safe:

>>> from threading import Thread
>>> x = iter(range(4))
>>> def doit(x):
...     print("result =", next(x))
...
>>> threads = [Thread(target=doit, args=(x,)) for i in range(4)]
>>> for t in threads:
...     t.start()
...
result = 0
result = 1
result = 2
result = 3
>>>



-- 
Steven



More information about the Python-list mailing list