1、time_limit和soft_time_limit區別
time_limit : 執行超時,結束signal 9 (SIGKILL) 執行的子進程,狀態:"status": "FAILURE" soft_time_limit :執行超時,用一個異常SoftTimeLimitExceeded來捕捉,狀態:"status": "SUCCESS"
目前只能在linux操作系統才有效
2、在裝飾器里面指定超時時間
2.1、time_limit 示例
@shared_task(time_limit=10) def handler_upload_data(file_path): """ 處理分析上傳文件 :param file_path: :return: """ ret = { 'code': 0, 'msg': '' } import time time.sleep(20) ret['data'] = { 'file_path': file_path } print('正常處理') return ret
運行效果
2.2、soft_time_limit示例
@shared_task(soft_time_limit=10) def handler_upload_data(file_path): """ 處理分析上傳文件 :param file_path: :return: """ ret = { 'code': 0, 'msg': '' } try: import time time.sleep(20) ret['data'] = { 'file_path': file_path } print('正常處理') except SoftTimeLimitExceeded: ret['code'] = 1 ret['msg'] = '處理超時,請重新上傳' return ret
運行效果
3、在調用任務的時候指定超時時間[簡單介紹]
handler_upload_data.apply_async((full_path,), soft_time_limit=10)
handler_upload_data.apply_async((full_path,), time_limit=10)