I have a multithreaded program where I create a generator function and then pass it to new threads. I want it to be shared/global in nature so each thread can get the next
Courtesy of IIRC python freenode, here is a working solutions for python 3.x
Generators are not thread safe by default, but heres how to make them to be thread safe
def my_generator():
while True:
for x in range(10):
yield x
class LockedIterator(object):
def __init__(self, it):
self._lock = threading.Lock()
self._it = iter(it)
def __iter__(self):
return self
def __next__(self):
with self._lock:
return next(self._it)
n = LockedIterator(my_generator)
next(n)
next(n)
next(n)
OR use a function
def threadsafe_iter(iterable):
lock = threading.Lock()
iterator = iter(iterable)
while True:
with lock:
for value in iterator:
break
else:
return
yield value
n = threadsafe_iter(my_generator)
next(n)
next(n)
next(n)