首页 > 编程语言 > 详细

python 使用队列实现线程同步

时间:2019-07-06 12:31:06      阅读:95      评论:0      收藏:0      [点我收藏+]
#通过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))

 

python 使用队列实现线程同步

原文:https://www.cnblogs.com/callyblog/p/11142098.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!