You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GlueJob执行完成触发PySpark InheritableThread导入异常求助

问题描述

Glue Job执行完毕时,每个使用PySpark InheritableThread创建的线程都会触发以下异常:

ImportError: sys.meta_path is None, Python is likely shutting down
Exception ignored in: <function InheritableThread.del at 0x7f0b958177a0>
Traceback (most recent call last):
File "/opt/amazon/spark/python/lib/pyspark.zip/pyspark/util.py", line 321, in del
ImportError: sys.meta_path is None, Python is likely shutting down

该异常不影响业务逻辑,但会输出冗余日志。已尝试thread.join()、time.sleep(12)、设置线程为daemon等方法,均无法解决。

解决方案

这个异常的根源是:Python解释器开始关闭时,sys.meta_path已被清空,而InheritableThread的__del__方法仍在尝试执行依赖模块导入的操作,从而触发ImportError。

方法:重写InheritableThread的__del__方法

通过继承InheritableThread并自定义__del__,在Python关闭阶段跳过可能触发异常的父类逻辑:

import sys
from pyspark.util import InheritableThread

class SafeInheritableThread(InheritableThread):
    def __del__(self):
        # 仅当Python未进入关闭阶段时,执行父类的清理逻辑
        if sys.meta_path is not None:
            super().__del__()

后续所有线程都使用SafeInheritableThread创建,即可避免该异常输出。

为什么之前的方法无效?

  • thread.join():仅等待线程执行完成,但线程对象销毁时仍会触发__del__,此时主程序可能已进入关闭阶段
  • time.sleep():仅延迟主程序退出时间,无法阻止解释器关闭时sys.meta_path被清空
  • 设置daemon线程:daemon线程随主进程退出,但线程对象的__del__仍会在解释器关闭阶段被调用

内容的提问来源于stack exchange,提问作者Rohit Chandravanshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:36:23