python - 为什么我们需要 Celery 中的签名?

标签 python django celery signatures

我已经开始在我的 Django Python 项目中使用 Celery 4.1 并且遇到了签名。

documentation它说了以下内容:

You just learned how to call a task using the tasks delay method in the calling guide, and this is often all you need, but sometimes you may want to pass the signature of a task invocation to another process or as an argument to another function.

A signature() wraps the arguments, keyword arguments, and execution options of a single task invocation in a way such that it can be passed to functions or even serialized and sent across the wire.

虽然我看到它们在一些示例中使用,但我真的不知道何时以及为何使用它们,以及它们解决了哪些问题。 有人可以向外行人解释一下吗?

最佳答案

签名与链一起使用以创建工作流。 “.s”是“.signature”的缩写。当使用“.s”时,表示将前端任务的结果或返回值传递给下一个任务。 “签名”的对立面是“不可变签名”,其中每个任务都是独立的。 例如(签名):

res = chain(add.s(2,2), add.s(4), add.s(8))
res().get()
>> 16

示例(不可变签名):

res = chain(add.si(2,2)|add.si(4,4)|add.si(8,8))()
res.get()
>>16
res.parent.get()
>>8
res.parent.parent.get()
>>4

关于python - 为什么我们需要 Celery 中的签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48098829/

相关文章:

python - 为什么它找不到我的 celery 配置文件?

python - celery /SQS错误 "No module named sqs"

python - 如何在 Pandas 的 groupby 之后获取列的计数百分比

python - django按当前用户过滤投票

python - Django -- 'data' 不是注册的命名空间

python - 在 Flask 中实现单个 worker 的最佳方式

Python ThreadPoolExecutor 抑制异常

Python编辑xml文件

python - 您可以从 Django ORM 的父类(super class)模型中访问子类模型吗?

python - gunicorn 在源更改时自动重新加载