Are Generators Threadsafe?

后端 未结 6 860
闹比i
闹比i 2020-12-02 09:29

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

6条回答
  •  温柔的废话
    2020-12-02 09:46

    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)
    

提交回复
热议问题