zoukankan      html  css  js  c++  java
  • 六、Celery任务执行超时的设置

    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)
  • 相关阅读:
    MongoDB 3.0 添加用户
    MongoDB基本命令用
    http://www.bootcss.com/
    UML之用例图
    .net 下分布式缓存(Memcached)实现
    Android系列 -- 2、视图组件View
    Android系列 -- 1、 初识android
    TP50、TP90、TP99、TP999详解
    Linux下SSD缓存加速之bcache使用
    数据中心常见电源线详细介绍
  • 原文地址:https://www.cnblogs.com/ygbh/p/13754580.html
Copyright © 2011-2022 走看看