zoukankan      html  css  js  c++  java
  • python 使用队列实现线程同步

    #通过queue的方式进行线程间同步,Queue在底层通过实现了dqueue(双生队列,在字节码时实现了线程安全)实现了线程安全
    from queue import Queue
    
    
    import time
    import threading
    
    
    def get_detail_html(queue):
        #爬取文章详情页
        while True:
            url = queue.get()# 如果没有数据会一直阻塞在这
            # for url in detail_url_list:
            print("get detail html started")
            time.sleep(2)
            print("get detail html end")
    
    
    def get_detail_url(queue):
        # 爬取文章列表页
        while True:
            print("get detail url started")
            time.sleep(4)
            for i in range(20):
                queue.put("http://projectsedu.com/{id}".format(id=i))#没有多余的位置时,会一直阻塞在这,直到有空闲的位置
            print("get detail url end")
    
    
    #1. 线程通信方式- 共享变量
    
    if  __name__ == "__main__":
        detail_url_queue = Queue(maxsize=1000)
    
    
        thread_detail_url = threading.Thread(target=get_detail_url, args=(detail_url_queue,))
        for i in range(10):
            html_thread = threading.Thread(target=get_detail_html, args=(detail_url_queue,))
            html_thread.start()
        start_time = time.time()
    
        detail_url_queue.task_done() # 队列任务完成,只有调用这个,线程才会退出
        detail_url_queue.join() # 阻塞主线程,调用了task_done,queue才会退出,不然一直会阻塞在queue这
    
        #当主线程退出的时候, 子线程kill掉
        print ("last time: {}".format(time.time()-start_time))
  • 相关阅读:
    并查集
    博弈——Bash、Nim、Wythoff
    博弈——SG函数模板
    数据结构——二叉树
    数据结构——链式队列
    sonar-maven-plugin问题
    确立核心模型
    调度思路+EurekaServer获得当前机器的instanceid
    简易的RestClient代码
    Spring Cloud App(Service) Pom示例
  • 原文地址:https://www.cnblogs.com/callyblog/p/11142098.html
Copyright © 2011-2022 走看看