網站首頁 編程語言 正文
python ThreadPoolExecutor線程池的工作線程中出現異常時,主線程不會捕獲異常。
解決方法1:
直接在需要執行的任務方法中添加try:
executor = ThreadPoolExecutor()
executor.submit(test_work, 0)
def test_work(p):
?? ?try:
?? ??? ?1/p
?? ?except Exception as e:
? ? ? ? logger.exception(e)
解決方法2:
添加完成運行時的callback:
executor = ThreadPoolExecutor()
task = executor.submit(test_work, 0)
task.add_done_callback(handle_exception)
handle_exception中又可以通過兩種方式捕獲異常:
2.1 通過concurrent.futures.Future.exception(timeout=None)
def handle_exception(worker):
# Method 1: concurrent.futures.Future.exception(timeout=None)
worker_exception = worker.exception()
if worker_exception:
logger.exception(worker_exception)
2.2 通過concurrent.futures.Future.result(Timeout = None)
def handle_exception(worker):
Method 2: try
try:
worker.result()
except Exception as e:
logger.exception(e)
原文鏈接:https://blog.csdn.net/ldahual/article/details/128134915
相關推薦
- 2022-11-06 Matplotlib學習筆記之plt.xticks()用法_python
- 2023-10-16 Echart 數據更新了,X軸或者Y軸顯示不變化的問題
- 2022-04-21 R語言繪制帶ErrorBar的分組條形圖代碼的分享_R語言
- 2023-05-07 oracle中如何保留兩位小數_oracle
- 2023-02-17 C++中二叉堆排序詳解_C 語言
- 2022-09-23 python人工智能自定義求導tf_diffs詳解_python
- 2022-04-11 圖文詳解C#中的協變與逆變_C#教程
- 2022-08-28 解決ffmpeg讀取視頻流時,報錯“Segmentation fault (core dumped)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支