解析为什么使用celery的task装饰就有delay属性

news2024/9/9 5:15:50

分析task装饰器原理

from celery.task import periodic_task

task源码如下

def task(*args, **kwargs):
    """Deprecated decorator, please use :func:`celery.task`."""
    return current_app.task(*args, **dict({'base': Task}, **kwargs))

这里回调用celery.app.base.Celery.task

附上源码

    def task(self, *args, **opts):
        """Decorator to create a task class out of any callable.

        See :ref:`Task options<task-options>` for a list of the
        arguments that can be passed to this decorator.

        Examples:
            .. code-block:: python

                @app.task
                def refresh_feed(url):
                    store_feed(feedparser.parse(url))

            with setting extra options:

            .. code-block:: python

                @app.task(exchange='feeds')
                def refresh_feed(url):
                    return store_feed(feedparser.parse(url))

        Note:
            App Binding: For custom apps the task decorator will return
            a proxy object, so that the act of creating the task is not
            performed until the task is used or the task registry is accessed.

            If you're depending on binding to be deferred, then you must
            not access any attributes on the returned object until the
            application is fully set up (finalized).
        """
        if USING_EXECV and opts.get('lazy', True):
            # When using execv the task in the original module will point to a
            # different app, so doing things like 'add.request' will point to
            # a different task instance.  This makes sure it will always use
            # the task instance from the current app.
            # Really need a better solution for this :(
            from . import shared_task
            return shared_task(*args, lazy=False, **opts)

        def inner_create_task_cls(shared=True, filter=None, lazy=True, **opts):
            _filt = filter

            def _create_task_cls(fun):
                if shared:
                    def cons(app):
                        return app._task_from_fun(fun, **opts)
                    cons.__name__ = fun.__name__
                    connect_on_app_finalize(cons)
                if not lazy or self.finalized:
                    ret = self._task_from_fun(fun, **opts)
                else:
                    # return a proxy object that evaluates on first use
                    ret = PromiseProxy(self._task_from_fun, (fun,), opts,
                                       __doc__=fun.__doc__)
                    self._pending.append(ret)
                if _filt:
                    return _filt(ret)
                return ret

            return _create_task_cls

        if len(args) == 1:
            if callable(args[0]):
                return inner_create_task_cls(**opts)(*args)
            raise TypeError('argument 1 to @task() must be a callable')
        if args:
            raise TypeError(
                '@task() takes exactly 1 argument ({0} given)'.format(
                    sum([len(args), len(opts)])))
        return inner_create_task_cls(**opts)

不用管上面的废话,重点是返回这个对象

ret = PromiseProxy(self._task_from_fun, (fun,), opts,
                                       __doc__=fun.__doc__)

他是调用父类的实例化

class Proxy(object):
    """Proxy to another object."""

    # Code stolen from werkzeug.local.Proxy.
    __slots__ = ('__local', '__args', '__kwargs', '__dict__')

    def __init__(self, local,
                 args=None, kwargs=None, name=None, __doc__=None):
        object.__setattr__(self, '_Proxy__local', local)
        object.__setattr__(self, '_Proxy__args', args or ())
        object.__setattr__(self, '_Proxy__kwargs', kwargs or {})
        if name is not None:
            object.__setattr__(self, '__custom_name__', name)
        if __doc__ is not None:
            object.__setattr__(self, '__doc__', __doc__)

这个时候设置了三个属性_Proxy__local就是传递进来的self._task_from_fun,_Proxy__args设置为args_Proxy__kwargs设置为kwargs,回到一开始代码看argskwargskwargs里面有一个base=Task而这个Task就是celeryTask类,他里面有delay方法,Task类位置在这celery.app.task.Task源码太多就不附上了
在这里插入图片描述

当调用delay属性因为不存在,使用调用了魔术方法,他重写了对应的魔术方法如下

    def __getattr__(self, name):
        if name == '__members__':
            return dir(self._get_current_object())
        return getattr(self._get_current_object(), name)

这里从self._get_current_object()中获取name属性返回,这里的name就是delay
接下来分析self._get_current_object()

    def _get_current_object(self):
        """Get current object.

        This is useful if you want the real
        object behind the proxy at a time for performance reasons or because
        you want to pass the object into a different context.
        """
        loc = object.__getattribute__(self, '_Proxy__local')
        if not hasattr(loc, '__release_local__'):
            return loc(*self.__args, **self.__kwargs)
        try:  # pragma: no cover
            # not sure what this is about
            return getattr(loc, self.__name__)
        except AttributeError:  # pragma: no cover
            raise RuntimeError('no object bound to {0.__name__}'.format(self))

重点就是获取loc,loc = object.__getattribute__(self, '_Proxy__local')这里获取到这个属性就是上面在初始化设置的_task_from_fun方法,接下来return loc(*self.__args, **self.__kwargs),他使用type创建一个task类并返回,所以现在那个task方法变成了一个task类他就有delay方法,附上_task_from_fun方法源码

    def _task_from_fun(self, fun, name=None, base=None, bind=False, **options):
        if not self.finalized and not self.autofinalize:
            raise RuntimeError('Contract breach: app not finalized')
        name = name or self.gen_task_name(fun.__name__, fun.__module__)
        base = base or self.Task

        if name not in self._tasks:
            run = fun if bind else staticmethod(fun)
            task = type(fun.__name__, (base,), dict({
                'app': self,
                'name': name,
                'run': run,
                '_decorated': True,
                '__doc__': fun.__doc__,
                '__module__': fun.__module__,
                '__header__': staticmethod(head_from_fun(fun, bound=bind)),
                '__wrapped__': run}, **options))()
            # for some reason __qualname__ cannot be set in type()
            # so we have to set it here.
            try:
                task.__qualname__ = fun.__qualname__
            except AttributeError:
                pass
            self._tasks[task.name] = task
            task.bind(self)  # connects task to this app
            add_autoretry_behaviour(task, **options)
        else:
            task = self._tasks[name]
        return task

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1553624.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JavaSE day14笔记

第十四天课堂笔记 课上: 适当做笔记课下 : 总结 , 读代码 , 反复敲代码 , 做练习 数组★★★ 数组 : 存储多个 同一类型 的容器格式 :数组类型 : 引用数据类型, new运算符在堆中 分配一块连续的存储空间 , 系统会给数组元素默认初始化 , 将该数组的引用赋值给数组名 引用数据…

如何在Win10使用IIS服务搭建WebDAV网站并实现无公网IP访问内网文件内容

文章目录 前言1. 安装IIS必要WebDav组件2. 客户端测试3. 使用cpolar内网穿透&#xff0c;将WebDav服务暴露在公网3.1 安装cpolar内网穿透3.2 配置WebDav公网访问地址 4. 映射本地盘符访问 前言 在Windows上如何搭建WebDav&#xff0c;并且结合cpolar的内网穿透工具实现在公网访…

腾讯云4核8G服务器价格,12M带宽一年646元,送3个月

2024年腾讯云4核8G服务器租用优惠价格&#xff1a;轻量应用服务器4核8G12M带宽646元15个月&#xff0c;CVM云服务器S5实例优惠价格1437.24元买一年送3个月&#xff0c;腾讯云4核8G服务器活动页面 txybk.com/go/txy 活动链接打开如下图&#xff1a; 腾讯云4核8G服务器优惠价格 轻…

ArcGIS矢量裁剪矢量

一、利用相交工具 Arctoolbox工具一分析工具一叠加分析一相交

Flink-CDC 无法增量抽取SQLServer数据

1.问题 因部署在WindowsServer服务器SQLServer发生过期后重启&#xff0c;Flink-CDC同步进行作业重启&#xff0c;启动后无报错信息&#xff0c;数据正常抽取。但是观察几天后发现当天数据计算指标无法展示 2.定位 因为没用进行任何修改&#xff0c;故初步判断不是因Flink-C…

【Java面试题】Redis上篇(基础、持久化、底层数据结构)

文章目录 基础1.什么是Redis?2.Redis可以用来干什么&#xff1f;3.Redis的五种基本数据结构&#xff1f;4.Redis为什么这么快&#xff1f;5.什么是I/O多路复用&#xff1f;6.Redis6.0为什么使用了多线程&#xff1f; 持久化7.Redis的持久化方式&#xff1f;区别&#xff1f;8.…

vsqt更改ui后cpp文件报错 原理

操作&#xff1a;我在ui界面改了一下&#xff0c;然后选中cpp文件右键编译->重新扫描&#xff08;或者全部重新生成&#xff09; 现象&#xff1a;这里一直红着&#xff0c;在UI命名空间&#xff0c;找不到PresetEvent 这个类 private:Ui::PresetEvent ui;//Ui::preset_eve…

机器学习入门:概念、步骤、分类与实践

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鸿蒙》 …

【JavaWeb】Day24.Web入门——HTTP协议(一)

HTTP协议——概述 1.介绍 HTTP&#xff1a;Hyper Text Transfer Protocol(超文本传输协议)&#xff0c;规定了浏览器与服务器之间数据传输的规则。 http是互联网上应用最为广泛的一种网络协议http协议要求&#xff1a;浏览器在向服务器发送请求数据时&#xff0c;或是服务器在…

hostapd代码分析

控制操作收包 hostapd_ctrl_iface_init函数中注册eloop回调&#xff1a; if (eloop_register_read_sock(hapd->ctrl_sock,hostapd_ctrl_iface_receive, hapd, NULL) <0) {hostapd_ctrl_iface_deinit(hapd);return -1;} hostapd_ctrl_iface_receive函数是hostapd接收报文…

指纹浏览器是什么?有哪些好用的推荐?

在网络世界中&#xff0c;保护您的在线隐私和安全非常重要。反检测浏览器是专门为此诞生的工具&#xff0c;旨在通过更改浏览器指纹来帮助您做到这一点&#xff0c;它们使网站、广告商和其他人很难跟踪您的在线行为。 一、什么是反检测浏览器&#xff1f; 您是否想过网站如何检…

AI21实验室推出Jamba模型,突破AI算力瓶颈,引领行业创新!

近日&#xff0c;AI领域再次迎来重大突破。知名AI研究实验室AI21在官网正式发布了其最新研究成果——基于SSM-Transformer混合架构的商业大模型Jamba。这款模型不仅继承了Transformer架构的优点&#xff0c;还巧妙地融合了SSM技术的优势&#xff0c;实现了性能和效率的双重飞跃…

Arthas-tarce线上排查问题流程

入门文档&#xff1a;trace | arthas 1、jar下载和启动 连接curl -O https://arthas.aliyun.com/arthas-boot.jar【wget https://arthas.aliyun.com/arthas-boot.jar】 。.../jdk/bin/java -jar arthas-boot.jar 22336【最好在这个目录启动,port可选】 选择进程序号 enter回车…

Linux:详解TCP协议段格式

文章目录 认识TCPTCP协议段格式 本篇主要总结的是TCP协议的一些字段 认识TCP TCP协议全称是传输控制协议&#xff0c;也就是说是要对于数据的传输进行一个控制 以上所示的是对于TCP协议进行数据传输的一个理解过程 全双工 至此就可以对于TCP协议是全双工的来进行理解了&…

WinServer启用Hyper-V新建虚拟机没有网络、无法开启增强模式

没有网络问题如下&#xff1a; 原因&#xff1a;没有在Hyper-V中新增交换机 操作—虚拟交换机管理器—新建虚拟网络交换机-外部-允许管理员操作系统共享此网络适配器 无法开启增强模式&#xff1a;

【分布式】——分布式事务

分布式事务 ⭐⭐⭐⭐⭐⭐ Github主页&#x1f449;https://github.com/A-BigTree 笔记链接&#x1f449;https://github.com/A-BigTree/tree-learning-notes ⭐⭐⭐⭐⭐⭐ Spring专栏&#x1f449;https://blog.csdn.net/weixin_53580595/category_12279588.html SpringMVC专…

免费在线制流程图和思维导图ProcessOn高效协同

免费在线制流程图和思维导图ProcessOn高效协同&#xff0c;还有海量的免费模板。在ProcessOn&#xff0c;你可以与全球超过1.1亿的优秀人才一起合作&#xff0c;共同绘制出精美的流程图和思维导图。无论是工作中的项目管理&#xff0c;还是学习中的知识整理&#xff0c;Process…

【计算机图形学】3D Implicit Transporter for Temporally Consistent Keypoint Discovery

对3D Implicit Transporter for Temporally Consistent Keypoint Discovery的简单理解 文章目录 1. 现有方法限制和文章改进2. 方法2.1 寻找时间上一致的3D特征点2.1.1 3D特征Transporter2.1.2 几何隐式解码器2.1.3 损失函数 2.2 使用一致特征点的操纵 1. 现有方法限制和文章改…

【热门话题】Yarn:新一代JavaScript包管理器的安装与使用

&#x1f308;个人主页: 鑫宝Code &#x1f525;热门专栏: 闲话杂谈&#xff5c; 炫酷HTML | JavaScript基础 ​&#x1f4ab;个人格言: "如无必要&#xff0c;勿增实体" 文章目录 Yarn&#xff1a;新一代JavaScript包管理器的安装与使用引言一、Yarn的安装1. 系…

FPGA结构与片上资源

文章目录 0.总览1.可配置逻辑块CLB1.1 6输入查找表&#xff08;LUT6&#xff09;1.2 选择器&#xff08;MUX&#xff09;1.3 进位链&#xff08;Carry Chain&#xff09;1.4 触发器&#xff08;Flip-Flop&#xff09; 2.可编程I/O单元2.1 I/O物理级2.2 I/O逻辑级 3.布线资源4.其…