如何停止/退出 AWS Glue 作业 (PySpark)?

cal*_*lor 6 amazon-web-services aws-glue aws-glue-spark

我成功运行了一个 AWS Glue 作业,用于转换数据以进行预测。如果达到特定条件,我想停止处理并输出状态消息(正在运行):

if specific_condition is None:
    s3.put_object(Body=json_str, Bucket=output_bucket, Key=json_path )
    return None
Run Code Online (Sandbox Code Playgroud)

这会产生“SyntaxError:'return'外部函数”,我尝试过:

if specific_condition is None:
    s3.put_object(Body=json_str, Bucket=output_bucket, Key=json_path )
    job.commit()
Run Code Online (Sandbox Code Playgroud)

这不是在 AWS Lambda 中运行,而是使用 Lambda 启动的胶水作业(例如 start_job_run())。

Jér*_*émy 6

由于@amsh的解决方案对我不起作用,我继续寻找解决方案并发现:

os._exit()立即在 C 层终止,并且不执行解释器的任何正常拆卸。

感谢@Glyph的回答!然后您可以按照以下方式继续:

if specific_condition is None:
    s3.put_object(Body=json_str, Bucket=output_bucket, Key=json_path )
    job.commit()
    os._exit()
Run Code Online (Sandbox Code Playgroud)

您的作业将成功,并且不会因“SystemExit:0”错误而终止。

  • 我使用 Glue 3.0 和 Python 3.9 时遇到以下错误:`TypeError: _exit() 缺少必需的参数 'status' (pos 1)`。但通过运行 os._exit(os.EX_OK) 修复了它 (2认同)