多线程终止处理

This commit is contained in:
dongchangxi committed 2025-12-05 10:37:17 +08:00
1 parent fd1de8c3d0
commit 6578f4732f
2 files changed
+37

No files matched your search

+9
View File
@@ -72,7 +72,9 @@ def main(work_dir=None):
exit(0)
# 循环处理,直到队列为空
try:
while True:
try:
r = redisClient()
#检测队列是否有值
if r.scard('pb:sliceing') == 0:
@@ -112,6 +114,13 @@ def main(work_dir=None):
print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} Redis连接检查失败: {str(e)},将在下次循环时自动重连')
#time.sleep(10)
except KeyboardInterrupt:
# 在循环内部捕获 KeyboardInterrupt,允许在 sleep 或操作中被中断
raise # 重新抛出,让外层捕获
except KeyboardInterrupt:
print(f'\n{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} 收到中断信号,正在优雅退出...')
print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} 程序已停止')
sys.exit(0)
def testMain():
+28
View File
@@ -438,6 +438,7 @@ def downloadDataByOssAndTransformSave(dirNewName, isSmallMachine=False, max_work
beginTime = time.time()
# 使用线程池并发处理
try:
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# 提交所有任务
future_to_item = {
@@ -448,6 +449,7 @@ def downloadDataByOssAndTransformSave(dirNewName, isSmallMachine=False, max_work
# 收集结果
success_count = 0
fail_count = 0
try:
for future in as_completed(future_to_item):
v = future_to_item[future]
try:
@@ -459,10 +461,33 @@ def downloadDataByOssAndTransformSave(dirNewName, isSmallMachine=False, max_work
fail_count += 1
log(f"处理失败: {v.get('file_name', 'unknown')}, 错误: {error_msg} ({fail_count}/{len(listData)})")
# 如果任何一个任务失败,记录错误但继续处理其他任务
except KeyboardInterrupt:
# 收到中断信号,取消所有未完成的任务
log(f"收到中断信号,正在取消未完成的任务...")
for f in future_to_item:
f.cancel()
raise # 重新抛出,让外层捕获
except Exception as e:
fail_count += 1
error_msg = f"处理数据项时发生异常: {str(e)}"
log(f"处理异常: {v.get('file_name', 'unknown')}, 错误: {error_msg} ({fail_count}/{len(listData)})")
except KeyboardInterrupt:
# 收到中断信号,取消所有未完成的任务
log(f"收到中断信号,正在取消未完成的任务...")
for f in future_to_item:
f.cancel()
# 尝试等待正在执行的任务完成(非阻塞方式)
import concurrent.futures
for f in list(future_to_item.keys()):
if not f.done():
try:
# 尝试获取结果,如果任务还在执行则立即返回
f.result(timeout=0.1)
except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError):
pass
except:
pass
raise # 重新抛出,让外层捕获
endTime = time.time()
log(f"多线程处理完成, 总耗时{endTime - beginTime}秒, 成功:{success_count}, 失败:{fail_count}, 总计:{len(listData)}")
@@ -473,6 +498,9 @@ def downloadDataByOssAndTransformSave(dirNewName, isSmallMachine=False, max_work
return False
return True
except KeyboardInterrupt:
log(f"多线程处理被中断")
raise # 重新抛出,让调用者处理
def findBpyModule():