Skip to content

Nested Chains Hang #1120

Closed
Closed

Description

@chrisw1229

I created a very simple test case to reproduce the problem I'm seeing with a more complex workflow. Basically, when I nest chains inside of other chains, then it only processes the first nested chain and never goes on to the next ones.

Task Code:

from celery import chain, task

def chain_bug():
    chain1 = chain(task1.si())
    chain2 = chain(task2.si())

    workflow = chain(chain1, chain2)
    workflow()
    return True

@task()
def task1():
    return True

@task()
def task2():
    return True

Environment:
Windows 7 x64
Python 2.7.3
Django 1.4.3
Celery 3.0.12
RabbitMQ 3.0.0
SQLite DB

Celery Settings:
BROKER_URL = 'amqp://guest:guest@localhost:5672//'
CELERY_RESULT_BACKEND = 'database'

Worker Command:
python manage.py celeryd --loglevel=info --concurrency=1

Worker Output:
[2012-12-10 19:35:35,286: INFO/MainProcess] consumer: Connected to amqp://guest@
127.0.0.1:5672//.
[[2012-12-10 19:35:35,503: INFO/MainProcess] child process calling self.run()
[2012-12-10 19:35:38,767: INFO/MainProcess] Got task from broker: processing.tas
ks.task1[35dd407c-1615-45f3-8d61-9e27aed4dc9a]
[2012-12-10 19:35:38,829: INFO/MainProcess] Task processing.tasks.task1[35dd407c
-1615-45f3-8d61-9e27aed4dc9a] succeeded in 0.0199999809265s: True

Activity

  1. AnonymousHelper commented on Dec 11, 2012

    @AnonymousHelper

    Chains can now chain other chains and use partial arguments (Issue #1057).
    Upgrade to celery 3.0.13

  2. chrisw1229 commented on Dec 11, 2012

    @chrisw1229
    Author

    Okay thanks.

  3. chrisw1229 commented on Dec 11, 2012

    @chrisw1229
    Author

    I'm also seeing another possibly related issue where a chain executes nested elements out of order. Would you expect that to be related to the same issue or should I file a separate issue?

        group1 = group(task1.si(), task1.si(), task1.si())
        group2 = group(task2.si(), task2.si(), task2.si())
    
        workflow = chain(group1, group2, task3.si())

    In that example, task 3 executes between some of the task 2 instances, which is not what I would expect.

  4. w- commented on Feb 27, 2013

    @w-
    Contributor

    i am seeing this too. any comment on this?

  5. AnonymousHelper commented on Mar 1, 2013

    @AnonymousHelper

    If you want to execute large number of tasks in order, you should refactor your workflow to use a Map instead of a chain. I ran into this same problem, and your best bet is to stay clear of chains.

  6. ask commented on Mar 4, 2013

    @ask
    Contributor

    @chrisw1229 @w- Simple explanation for this is that 'group' is a task itself, which operation is to send the task messages. It's implemented similar to this:

    @task
    def group(tasks):
        with app.producer_or_acquire() as producer:
            for signature in tasks:
                signature.apply_async(publisher=producer)
    

    So in the case of:

    chain(group1, group2, task3.si())
    

    group2 will execute after the tasks in the group1 group has been applied,
    not when the tasks in that group complete.

    In any case you cannot synchronize on a group, that's what chords are for!

  7. ask commented on Mar 4, 2013

    @ask
    Contributor

    And I totally forgot that chained groups are automatically upgraded to a chord. So disregard the last reply (the details of group is still true though, just that chain() will rewrite them to be a chord)

  8. chrisw1229 commented on Mar 4, 2013

    @chrisw1229
    Author

    Yes, thanks ask. During my experiments I could see from the worker output that certain combinations were creating chords under the covers, which is why I thought there could be a bug here. I was able to get my example working by converting everything to chords manually. I haven't gotten to try this example on the most recent version of Celery to see if the problem persists, but I should have time to look into this again soon. It's not a huge deal if the answer is just to use chords, but I find the chain/group APIs more clear to understand.

  9. chrisw1229 commented on Apr 5, 2013

    @chrisw1229
    Author

    @ask I finally got around to trying this again under Celery 3.0.17 and it looks like there are still bugs. The following snippet still never runs task2 after task1 completes. I also tried adding extra task1 and task2 subtasks to each of the chains and that didn't help.

        chain1 = chain(task1.si())
        chain2 = chain(task2.si())
    
        workflow = chain(chain1, chain2)

    I also re-tested the nested groups issue I mentioned in an earlier comment and that still runs the tasks out of order too. Celery is definitely upgrading the nested groups to use 2 chords, but for some reason it doesn't behave the same as when I implement it explicitly myself. So this code does not work as expected:

        group1 = group(task1.si(), task1.si(), task1.si())
        group2 = group(task2.si(), task2.si(), task2.si())
    
        workflow = chain(group1, group2, task3.si())

    Re-writing it like this does work correctly though. It's just not quite as readable in my opinion.

        group1 = [task1.si(), task1.si(), task1.si()]
        group2 = [task2.si(), task2.si(), task2.si()]
    
        workflow = chord(group1, chord(group2, task3.si()))

    I understand the workflow nesting can get complicated to implement, but I think if we can work out these types of issues Celery provides the most power/flexibility along with the simplest API compared to anything else I've seen.

  10. w- commented on Apr 9, 2013

    @w-
    Contributor

    in case this helps you, a cleaner way of being explicit is detailed here:
    #1214 (comment)

  11. ask commented on Oct 7, 2013

    @ask
    Contributor

    @chrisw1229 The first chain example you had problems with works now in 3.1rc

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