美文网首页
11-3 Python的线程通信Queue

11-3 Python的线程通信Queue

作者: 正在努力ing | 来源:发表于2018-08-26 15:39 被阅读0次
    import threading
    import time
    
    global_list = []
    
    def get_html(global_list):
        while True:
            if(len(global_list)):
                item = global_list.pop()
                print(item)
            else:
                print("No item to show")
    
    def get_url(global_list):
        for i in range(100):
            global_list.append(i)
    
    
    thread1 = threading.Thread(target=get_url,args=(global_list,))
    thread2 = threading.Thread(target=get_html,args=(global_list,))
    #thread3 = threading.Thread(target=get_html,args=(global_list,))
    
    thread1.start()
    thread2.start()
    #thread3.start()
    
    
    import threading
    from queue import Queue
    
    global_list = []
    
    def get_html(queue):
        status = 0
        while True:
            url = queue.get()
            if (status == 2):
                break
            if(url):
                print("url is ",url)
            else:
                status +=1
    
    def get_url(queue):
    
        for i in range(1000):
            queue.put(i)
            print("i is ",i)
    
    if __name__ == "__main__":
        news_queue = Queue(maxsize=1000)
        thread1 = threading.Thread(target=get_url,args=(news_queue,))
        thread2 = threading.Thread(target=get_html,args=(news_queue,))
        #thread3 = threading.Thread(target=get_html,args=(global_list,))
    
        thread1.start()
        thread2.start()
        #thread3.start()
        
    >>>  
    i is  0
    ...
    i is  9
    url is  1
    i is  10
    i is  11
    ...
    i is  31
    i is  32
    url is  2
    i is  33
    i is  34
    
    

    相关文章

      网友评论

          本文标题:11-3 Python的线程通信Queue

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