美文网首页
python queue的使用

python queue的使用

作者: Do_More | 来源:发表于2017-07-15 15:45 被阅读0次
    import threading
    import time
    from queue import Queue
    
    def job(l,q):
        for i in range(len(l)):
            l[i] = l[i] ** 2
        q.put(l)
    
    def multithreading():
        q = Queue()
        threads = []
        data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
        for i in range(4):
            t = threading.Thread(target=job, args=(data[i],q))
            t.start()
            threads.append(t)
        for thread in threads:
            thread.join()
        results = []
        for _ in range(4):
            results.append(q.get())
        print(results)
    
    
    if __name__ == '__main__':
        multithreading()
    

    相关文章

      网友评论

          本文标题:python queue的使用

          本文链接:https://www.haomeiwen.com/subject/ndxzhxtx.html