线程通信-通过queque的方式进行线程通信

左手的ㄟ右手 2023-07-17 14:19 90阅读 0赞
  1. from queue import Queue
  2. import time
  3. import threading
  4. def get_detail_html(queue):
  5. while True:
  6. url =queue.get()
  7. print(" get detail html started")
  8. time.sleep(2)
  9. print("get detail html end")
  10. def get_detail_url(queue):
  11. while True:
  12. print(" get detail url started")
  13. time.sleep(2)
  14. for i in range(20):
  15. queue.put("https://123.com")
  16. print("get detail url end")
  17. if __name__ == '__main__':
  18. detail_queue = Queue(maxsize=1000) # 创建一个消息队列
  19. # 创建一个线程给队列中添加数据
  20. thread_detail_url = threading.Thread(target=get_detail_url,args=(detail_queue,))
  21. # 开启10个线程并
  22. for i in range(10):
  23. html_thread = threading.Thread(target=get_detail_html,args=(detail_queue,))
  24. html_thread.start()
  25. # 队列阻塞直到执行完
  26. detail_queue.join()
  27. # detail_queue.task_done()# 退出

发表评论

表情:
评论列表 (有 0 条评论,90人围观)

还没有评论,来说两句吧...

相关阅读