简体   繁体   English

如果任何作业在 python 中失败,如何退出进程?

[英]How to exit the process if any job will fail in python?

I am running jobs in a parallel manner based on sequence number.我基于序列号以并行方式运行作业。 I am taking the status of every job like - success or Failed.我正在获取每项工作的状态,例如 - 成功或失败。 Then after getting the status of every job i am sending mail with status of every job.然后在获得每项工作的状态后,我将发送带有每项工作状态的邮件。

But mail is generating after finishing of whole process.但是邮件是在整个过程完成后生成的。 But i want if any job will fail the process will stop there and mail will generate.但我希望如果任何工作失败,该过程将停止并生成邮件。

Can you please help me how to do this?你能帮我怎么做吗?

Code i am running:我正在运行的代码:

        df_mail_final = pd.DataFrame()
        df_mail_final1 = pd.DataFrame()

        '''Getting the status of every job'''
        for m_job in df_main4.master_job.unique():
            list_df = []
            dict_mail = OrderedDict()
            temp_df1 = df_main4[df_main4['master_job'] == m_job].copy()
            temp_df1['duration'] = pd.to_datetime(temp_df1['end_time'].unique()[-1]) - pd.to_datetime(temp_df1['start_time'].unique()[0])
            temp_df1['duration'] = temp_df1['duration'].replace('0 days' ,'')
            status_list = temp_df1.status.unique()
            if(0 in status_list):
                dict_mail['Master Job Name'] = m_job
                idx = temp_df1['status'] == 0
                dict_mail['Execution_Seq'] = temp_df1.loc[idx]["exec_seq"].unique()[0]
                dict_mail['Start_time'] = temp_df1.loc[idx]["start_time"].unique()[0]
                dict_mail['End_time'] = temp_df1.loc[idx]["end_time"].unique()[-1]
                dict_mail['Status'] = 'Failed'
                dict_mail['Duration'] = temp_df1.loc[idx]["duration"].unique()[-1]
                dict_mail['Reason'] = temp_df1.loc[idx]["error_msg"].unique()[0]
                dict_mail['Function_Name'] = temp_df1.loc[idx]["error_func"].unique()[0]
                list_df.append(dict_mail)
                df_mail = pd.DataFrame(list_df)
            if(0 not in status_list):
                print(m_job)
                dict_mail['Master Job Name'] = m_job
                dict_mail['Execution_Seq'] = temp_df1.exec_seq.unique()[0]
                dict_mail['Start_time'] = temp_df1.start_time.unique()[0]
                dict_mail['End_time'] = temp_df1.end_time.unique()[-1]
                dict_mail['Status'] = 'Success'
                dict_mail['Duration'] = temp_df1.duration.unique()[-1]
                dict_mail['Reason'] = ''
                dict_mail['Function_Name'] = ''
                list_df.append(dict_mail)
                df_mail = pd.DataFrame(list_df)
            df_mail_final = pd.concat([df_mail_final,df_mail], axis=0, ignore_index=True)
            #if(df_mail_final['Status'].iloc[-1] == 'Failed'):
                #break

        '''Printing the Final Dataframe with status of all the jobs'''
        print(df_mail_final)
        df_mail_final = df_mail_final[['Master Job Name', 'Execution_Seq', 'Start_time', 'End_time', 'Status', 'Duration', 'Reason', 'Function_Name']]
        exec_end_dt = datetime.datetime.now().strftime("%H:%M:%S")
        #total_duration = pd.to_datetime(exec_end_dt) - pd.to_datetime(exec_start_dt)
        total_duration= pd.to_datetime(df_mail_final['End_time']).max() - pd.to_datetime(df_mail_final['Start_time']).min()
        total_duration = str(total_duration)
        total_duration = total_duration.replace('0 days', '')
        send_mail(df_mail_final, LOG_FILE, total_duration)

Sharing a gist/system design logic of how to implement this job分享如何实施这项工作的要点/系统设计逻辑

def my_parallel_job(*args, **kwargs):
    # do your stuff here
    pass

def parallel_job_wrapper(*args, **kwargs):
    try:
       my_parallel_job(*args, **kwargs)
       # if errors following will not run
       return "success"
    except:
      # if errors comes
      return "fail"

def main(*args, **kwargs):
    # call you parallel jobs from here
    p1 = parallel_job_wrapper(*args, **kwargs)
    # preferably you are using something like python's multithreading pool methods

In the above code, the second function is to act as a cushion, in case of any failure of the first function.在上面的代码中,第二个功能是作为缓冲的,以防第一个功能出现故障。 This ensures that your main does not stop even when any parallel job fails.这可确保即使任何并行作业失败,您的main也不会停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM