Skip to content

Support reusing evaluated signatures #893

Open

Description

@davemt

Consider the following (where A, B, C, D, E are tasks):

sub_A = A.s()
chord_D = chord([sub_A, B.s()], D.s())
chord_E = chord([sub_A, C.s()], E.s())
res = group(chord_D, chord_E)()

This creates two separate invocations for task A. For many cases, we would want the subtask for A to be invoked just once, and the work shared.

It seems that manually giving the subtask a uuid leads to the desired outcome, but it would be nice to have support for this in the API.

Activity

  1. ask commented on Jul 30, 2012

    @ask
    Contributor

    A subtask instance may be called multiple times, e.g:

    >>> add2 = add.s(2)
    
    >>> add2(4).get()
    6
    
    >>> c = chain(add.s(4), add.s(8))
    >>> c(4).get()
    16
    

    In addition chord, group, chain etc may clone the tasks, this to set new task_ids etc without affecting
    later calls.

  2. ask commented on Jul 30, 2012

    @ask
    Contributor

    oh and my point, which is that sub_A is called twice in your example.

  3. davemt commented on Jul 31, 2012

    @davemt
    Author

    Yes, and this is not always desired.

    It would be better illustrated as sub_A = A.s(<some specific input>). If the outcome of A is deterministic based on a particular input, we could share the result of one execution. However, as you said, sub_A is called twice.

    Doing this

    sub_A.options.setdefault('task_id', uuid())
    

    before we get() seems to work, but it would be nice if there was a more supported way of doing this.

  4. davemt commented on Aug 1, 2012

    @davemt
    Author

    I should add: I would expect the subtask to be called twice in the original snippit that I posted.

    The requested functionality would require some new way of denoting a subtask which could appear multiple times in a dependency graph, where we should share the single-execution result.

    This is much more straightforward for immutable subtasks. It seems things might get pretty tricky if the subtask was mutable.

  5. steeve commented on Aug 28, 2012

    @steeve
    Contributor

    What you're asking is caching:
    Have a task given a determinate task_id, which allows us not to execute it if the result is already here, or if it is already executing.

    But this is complicated for several reasons:

    • How to determine if the task is already running? We can interrogate the other workers, but this will lead to a race condition (if the worker starts executing A right after we ask for it, and we start it again anyway).
    • Some backends (AMQP) won't be able to support caching, ever.
  6. ask commented on Mar 5, 2013

    @ask
    Contributor

    I don't think you need to check if it's already running, just need some way to refer to a task that has already been called, e.g.:

    result = add.s(2, 2).delay()
    chord(result, add.s(8, 8)(xsum.s())
    

    This does not currently work, but you can imagine chord could skip sending a message if it encounters an AsyncResult instance and just use the id as-is.

    On top of that we would need a way to refer to the result of a subtask to combine it in expressions,
    as in the original example, this way the subtask is only called once and subsequent calls will return the same result instance, e.g:

    sub_A = freeze(A.s())
    chord_D = chord([sub_A, B.s()], D.s())
    
  7. modified the milestones: , v4.0 on Oct 31, 2015
  8. afarbos commented on Dec 5, 2015

    @afarbos

    I am not sure to get it. Does res_A will be executed twice in this case or not?

    res_A = A.s().freeze()
    group(chord([res_A , B.s()], D.s()), chord([res_A, C.s()], E.s()))

    And does the dependencies will be respected? In this A, B and C in parallel and only launch D when A and B are successful and E only when A and C are successful.

  9. ask commented on Dec 9, 2015

    @ask
    Contributor

    Yes!

  10. neoascetic commented on Mar 10, 2016

    @neoascetic

    Want this :)

  11. GlennS commented on Jun 22, 2016

    @GlennS

    Just a note that this appears to be the same issue as discussed here.

    I wasn't able to figure out how to pass a list of task_ids to the underlying chord implementation.

  12. ask commented on Jun 23, 2016

    @ask
    Contributor

    Closing this, as with current resources it will take 10 years to complete.

  13. 7 remaining items

  14. auvipy commented on Mar 19, 2021

    @auvipy
    Member

    I think we should move this to next milestone

  15. thedrow commented on Mar 21, 2021

    @thedrow
    Contributor

    I agree. This can be done only with an event loop as we have to check the backend first for a result which means waiting.
    This requires an event loop.

    In addition, it requires the task to be declared as idempotent which is something we currently don't support.

  16. jpsnyder commented on Mar 23, 2023

    @jpsnyder

    I just ran into this issue myself. Really putting a damper on my excitement to use celery.

    I was wondering if anybody knows a good workaround (without multiple blocking stages)?

    Basically we need a "reverse chord", where instead of the results of a group of tasks feeding into a single task, we have the result of a single task feeding into a group of many tasks.

    celery.reverse_chord(add.s(1, 2), [sub.s(3), sub.s(4), sub.s(5)]) 
    # sum = 1 + 2
    # result = [sum - 3, sum - 4, sum - 5]
  17. sjkoelle commented on Dec 6, 2023

    @sjkoelle

    Has there been any progress? Support for arbitrary DAGs would be fantastic.

  18. PythonFZ commented on Oct 26, 2024

    @PythonFZ

    Is this still on the Roadmap? I do have my project workflow as networkx graph and I am trying to run it using celery phasing exactly this issue.

  19. simonepozzoli-pix4d commented on May 24, 2025

    @simonepozzoli-pix4d
    Contributor

    I just ran into this issue myself. Really putting a damper on my excitement to use celery.

    I was wondering if anybody knows a good workaround (without multiple blocking stages)?

    Basically we need a "reverse chord", where instead of the results of a group of tasks feeding into a single task, we have the result of a single task feeding into a group of many tasks.

    celery.reverse_chord(add.s(1, 2), [sub.s(3), sub.s(4), sub.s(5)])
    sum = 1 + 2
    result = [sum - 3, sum - 4, sum - 5]

    Isn't it possible to use?

    chain(add.s(1, 2), group(sub.s(3), sub.s(4), sub.s(5)))
  20. auvipy commented on May 27, 2025

    @auvipy
    Member

    we need contributions here.

  21. deleted a comment from jpsnyder on Sep 12, 2026
  22. chrisbarber commented on Nov 3, 2026

    @chrisbarber
    Contributor

    With some abuse of the task message format, this can be achieved by basically making header tasks members of multiple “bodies” and patching on_chord_part_return (for the backends that use it) basically to just loop over them:

    from celery.backends.redis import RedisBackend
    orig_ocpr = RedisBackend.on_chord_part_return
    def new_ocpr(self, request, state, result, propagate=None, **kwargs):
        if isinstance(request.group, list):
            for group, group_index, chord in zip(request.group, request.group_index, request.chord):
                new_request = deepcopy(request)
                new_request.group = group
                new_request.group_index = group_index
                new_request.chord = chord
                orig_ocpr(self, new_request, state, result, propagate=propagate, **kwargs)
        else:
            orig_ocpr(self, request, state, result, propagate=propagate, **kwargs)
    RedisBackend.on_chord_part_return = new_ocpr

    Here’s a gist with some examples building up in complexity from two overlapping chords towards a general DAG: https://gist.github.com/chrisbarber/066d507b454de1dffb71a37e39d3c385

    Here’s a ~200 LOC DAG canvas-compatible element modelled after something we’ve been running in production for a year, also with proof-of-concept on that repo for how easy it is to make work with the non key-value (polling) backends: https://github.com/Carrot-Consortium/celery-dag

    In there I am more or less stuffing whatever I feel like in request.chord, so one may hesitate to run it in production, and we also use something slightly different. But in essence nothing more than making “group,” “group_index,” and “chord” into plural versions of themselves in the message format would suffice.

    I’m not 100% sure but I think the main blockers here could be easily overcome:

    1. I think the task/request message format would need to be extended (could be backwards-compatible) to support the plural forms of the above mentioned entities
    2. I’m a bit lost with the cloud of related open issues here, as there seem to be some abstract or elegant things like “reusable signatures” and whatnot being requested, when in fact maybe people just want pure DAG workflow support in Celery and it would be better to just deliver that, rather than finding some way to merge the concept of a DAG with Celery’s more historical/strict notions.

    Having proved the ease of DAG support with trivial monkey patches and fairly straightforward extensions to the message format, do you see some path to integrating something like this without ballooning the scope? I see it as huge potential for added value to get DAG support in Celery as a lot of people seem to want it; many seem be led to believe by the docs that Celery has it (perhaps simply because of the use of the term “workflows” on this page and the many graph references), and then have to painfully learn it does not.

  23. auvipy commented on Nov 4, 2026

    @auvipy
    Member

    I saw the code of your celery dag, I think that can be tried in celery core

  24. modified the milestones: Future, 6.2 on Nov 4, 2026
  25. self-assigned this
    on Nov 4, 2026
  26. modified the milestones: 6.2, 6.0 on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions