Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need Signatures in Celery?

I've started using Celery 4.1 in my Django Python project and have come across Signatures.

In the documentation it says the following:

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.

Although I see them used in some of the examples I don't really know when and why to use them, as well as which problems they solve. Can someone explain this to a layman?

like image 315
Monoclecat Avatar asked Jan 04 '18 15:01

Monoclecat


People also ask

What are celery signatures for?

What are Celery Signatures ? Signatures are used when you want to send the signature of a task invocation to another process or as an argument to another function. A signature() wraps the args, kwargs, and execution options of a single task invocation.

What is Shared_task in celery?

The "shared_task" decorator allows creation of Celery tasks for reusable apps as it doesn't need the instance of the Celery app. It is also easier way to define a task as you don't need to import the Celery app instance.

What is the purpose of celery Python?

Celery is an open-source Python library which is used to run the tasks asynchronously. It is a task queue that holds the tasks and distributes them to the workers in a proper manner. It is primarily focused on real-time operation but also supports scheduling (run regular interval tasks).

What is a celery chord?

Celery chords are one of the six Celery workflow primitives. A Celery workflow defines the order in which individual Celery tasks are executed asynchronously. A chord consists of a Celery group (called chord header) and a callback. A group is a list of tasks that are executed in parallel.


2 Answers

Signature used together with chain to create a workflow. ".s" is the abbreviation of ".signature". when use ".s", means that the result or return value of the front task will be pass to the next one. The opposite of 'signature' is 'immutable signature', in which every task is independent. for example(signature):

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

example(immutable signature):

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
like image 191
Leona.li Avatar answered Sep 26 '22 01:09

Leona.li


You can think of signatures in Celery as placeholders for running tasks. For example, let's say you wish to construct a complex workflow consisting of chords, groups and chains, and use it in a different piece of code. In this case, it would be easier to define various task signatures and place them in your workflow as neccessary:

def create_workflow():
    return chord([sig_1, sig_2, chain([sig_3, sig_4])], 
        body=group([sig_5, sig_6]).set(queue=PRIORITY_QUEUE))

Where each signature in this example has been pre-defined, which can ,make a lot of difference should those signatures be complicated. Then you can call create_workflow() and apply delay() to it whenever needed

like image 27
Jimmy Lee Jones Avatar answered Sep 26 '22 01:09

Jimmy Lee Jones