码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • WithThread多线程识别某一线程受阻


    从源代码修改

    1. from threading import Thread
    2. from queue import Queue
    3. class WithThread:
    4. def __init__(self, thread_num):
    5. self.thread_num = thread_num
    6. self.task_queue = Queue()
    7. self.threads = []
    8. self.is_running = False
    9. self.thread_status = [True] * thread_num # 新增一个列表记录每个线程的状态
    10. def put(self, func, args=()):
    11. if not self.is_running:
    12. self.start()
    13. self.task_queue.put((func, args))
    14. def start(self):
    15. self.is_running = True
    16. for i in range(self.thread_num):
    17. t = Thread(target=self._run, args=(i,)) # 将线程ID作为参数传入方法中
    18. t.start()
    19. self.threads.append(t)
    20. def stop(self):
    21. self.is_running = False
    22. for t in self.threads:
    23. t.join()
    24. def _run(self, i):
    25. while self.is_running:
    26. try:
    27. func, args = self.task_queue.get(timeout=1)
    28. except Exception as e:
    29. # 捕获Empty异常
    30. continue
    31. try:
    32. func(*args)
    33. except Exception as e:
    34. print("任务执行失败: ", e)
    35. finally:
    36. self.task_queue.task_done()
    37. self.thread_status[i] = True # 在任务完成后将状态标记为True
    38. def get_thread_status(self):
    39. return self.thread_status # 返回线程状态列表

    新增了一个thread_status列表来记录每个线程的状态,并在每个线程的_run方法中更新状态。另外,我们还新增了一个get_thread_status方法,用于获取所有线程的状态。

    1. from threading import Thread
    2. from queue import Queue
    3. class WithThread:
    4. def __init__(self, thread_num, max_queue_size=100):
    5. self.thread_num = thread_num
    6. self.task_queue = Queue()
    7. self.max_queue_size = max_queue_size
    8. self.threads = []
    9. self.is_running = False
    10. def put(self, func, args=()):
    11. if not self.is_running:
    12. self.start()
    13. self.task_queue.put((func, args))
    14. def start(self):
    15. self.is_running = True
    16. for i in range(self.thread_num):
    17. t = Thread(target=self._run)
    18. t.start()
    19. self.threads.append(t)
    20. def stop(self):
    21. self.is_running = False
    22. for t in self.threads:
    23. t.join()
    24. def _run(self):
    25. while self.is_running:
    26. try:
    27. func, args = self.task_queue.get(timeout=1)
    28. except Exception as e:
    29. # 捕获Empty异常
    30. continue
    31. try:
    32. func(*args)
    33. except Exception as e:
    34. print("任务执行失败: ", e)
    35. finally:
    36. self.task_queue.task_done()
    37. # 判断队列长度是否超出阈值
    38. if self.task_queue.qsize() > self.max_queue_size:
    39. print("当前线程受阻,队列长度:", self.task_queue.qsize())

    新增了一个max_queue_size参数,并在线程的_run方法中判断队列长度是否超过该阈值。如果队列长度超过阈值,就认为该线程受阻。

  • 相关阅读:
    青源Talk第8期|苗旺:因果推断,观察性研究和2021年诺贝尔经济学奖
    .net core 和 WPF 开发升讯威在线客服系统:调用有道翻译接口实现实时自动翻译的方法
    鸿蒙实战开发:数据交互【RPC连接】
    详情图怎么做二维码?批量线上图片生码图文教学
    How to get active Profiles in Spring
    牛客题目——链表中倒数最后k个结点、两个链表的第一个公共结点、二叉搜索树的最近公共祖先
    全真模拟题!PMP提分必练
    python-OpenCV-对图片进行操作
    Java代码审计前置知识——Spring框架AOP和IoC
    在学习DNS的过程中给我的启发
  • 原文地址:https://blog.csdn.net/EaSoNgo111/article/details/133929931
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号