Python: Why different threads get their own series of values from one generator?

后端 未结 2 698
刺人心
刺人心 2021-01-06 16:33

I\'m learning multithreading in Python. I want to know how to provide data to multiple threads using generators. Here\'s what I wrote:

  import threading

           


        
2条回答
  •  星月不相逢
    2021-01-06 17:15

    Can you just use Python Queue class. I believe that is thread safe. Basically you could just use your generator to fill the Queue and then have each thread pull from it.

    #!/usr/bin/python
    
    import Queue
    import threading
    
    queueOfNumbers = Queue.Queue()
    
    data = [i for i in xrange(100)]
    
    def generator():
        for i in data:
            yield i
    
    class CountThread(threading.Thread):
        def __init__(self, name, queue):
            threading.Thread.__init__(self)
            self.name = name
            self.queue = queue
    
        def run(self):
            i = self.queue.get()
            print '%s %s' % (self.name, i)
    
    
    a = CountThread('a', queueOfNumbers)
    b = CountThread('b', queueOfNumbers)
    a.start()
    b.start()
    
    for num in generator():
        queueOfNumbers.put(num)
    
    queueOfNumbers.join()
    

    http://www.ibm.com/developerworks/aix/library/au-threadingpython/

提交回复
热议问题