Skip to content

Creating a DAG in Celery #3666

Closed
Closed

Description

@matthewatabet

NOTE: This is not strictly a bug or feature request. It's quite possible Celery wasn't meant to handle this type of problem.

Is it possible to represent all DAGs using Celery canvases? My hunch is no, but I wanted to double check.

The graph I'd like to represent is something like:

a = A()
b = B(a)
c = C(a, b)

Where A, B, and C are tasks. So, C receives both the output of A and the output of B whilst B receives the output of A. Is something like this possible?

Activity

  1. ask commented on Dec 8, 2016

    @ask
    Contributor

    A workflow is definitely a tree structure, but I guess it's not a graph as long as it's impossible to refer to an existing task.

    There was a feature request wanting something like this to be possible:

    a = A.s()
    workflow = (a | B.s() | C.s() | a | D.s())

    but I'm not sure how that can logically work, considering that the return value of C cannot be passed
    on to A, when A already finished executing.

    If you merely want to refer to the result of a previously executed task, then that is definitely possible:

    a = A.s()
    a_result = a.freeze()
    
    workflow = (a | B.s() | C.s(a_result.as_tuple()))
    workflow.delay()

    At this point C would receive two arguments: the result of B and the result object (promise) of the A task:

    from celery.result import result_from_tuple
    
    @app.task(bind=True)
    def C(self, B_value, A_promise):
        A_promise = result_from_tuple(A_promise, app=self.app)
        A_value = A_promise.get(timeout=1)
    
        return A_value + B_value

    That's one way to do it, but you could also design a primitive that forwards results:

    @app.task(bind=True)
    def forward(self, result, sig):
        # convert JSON serialized signature back to Signature
        sig = self.app.signature(sig)
        # get the return value of the provided task signature
        result2 = sig()
        # next task will receive tuple of (result_A, result_B)
        return (result, result2)
    
    
    @app.task
    def C(self, (Ares, Bres)):
        return Ares + Bres
    
    workflow = (A.s() | forward.s(B.s()) | C.s())

    NOTE: Using unpacking in the function signature is not allowed in Python 3, there you'd have to do:

    @app.task
    def C(self, Ares__Bres):
        Ares, Bres = Ares__Bres
        return Ares + Bres
    
  2. matthewatabet commented on Dec 9, 2016

    @matthewatabet
    Author

    Great, thanks for the quick response. I like forward quite a bit. The downsides I see are that it copies the previous result and uses some worker time, but those probably don't matter in most situations.

    Thanks for the help!

  3. Yashg19 commented on Jun 12, 2017

    @Yashg19

    It looks like something changed in the latest release. @ask if I run the way you have mentioned (first way) I get an error "RuntimeError: Never call result.get() within a task! It refers to the document page which I have gone through. Any help will be appreciated here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions