Skip to content

group and chord: subtasks are not executed until entire generator is consumed - #3043

Closed
chadrik wants to merge 10 commits into
celery:masterfrom
chadrik:chord_generators
Closed

chadrik wants to merge 10 commits into
celery:masterfrom
chadrik:chord_generators

Conversation

@chadrik

@chadrik chadrik commented Feb 6, 2016

Copy link
Copy Markdown

When passing a generator to group or chord, sub-tasks are not submitted to the backend for execution until the entire generator is consumed. The expected result is that subtasks are submitted during iteration, as soon as they are yielded. This can have a big impact on performance if generators yield subtasks over a long period of time.

I also opened issue #3021 on the subject. This explanation from @Eli-Green I think is pretty on point.

So far I've only added support for redis. I started looking at the other backends but I ran out of time. It would be great to get some feedback on what I have so far and to get some thoughts on how difficult it will be to add for the other backends.

Add a __nonzero__ check that only consumes one item.
Add a __repr__ that does not consume any items.
Prior to this change, if a generator was passed to group or chord it was completely expanded prior to any tasks being submitted to the backend.  After this change, tasks will be submitted to the backend as they are retreived from the generator, allowing work to begin much sooner for long-running generators.
This is currently a work in progress: only redis is supported.
@thedrow

thedrow commented Feb 8, 2016

Copy link
Copy Markdown
Contributor

Do the tests already cover this usecase?

@chadrik

chadrik commented Feb 8, 2016

Copy link
Copy Markdown
Author

Do the tests already cover this usecase?

@thedrow no they don't. I haven't added any new tests, but I plan to once I get a nod of approval on the general idea from @ask or another maintainer (is there another maintainer?). Any help I could get on implementing the feature for the other backends would be appreciated as well.

@thedrow

thedrow commented Feb 8, 2016

Copy link
Copy Markdown
Contributor

I'm also maintaining the project but I never used celery with redis so I'm not familiar with that part of the code.

@chadrik

chadrik commented Feb 11, 2016

Copy link
Copy Markdown
Author

@thedrow Can you weigh in on how this might be done for the other backends? If they were completed, do you see any reason not to merge this PR?

@thedrow

thedrow commented Feb 13, 2016

Copy link
Copy Markdown
Contributor

I don't see any reason why this shouldn't be merged. It just requires tests.

@chadrik

chadrik commented Feb 14, 2016

Copy link
Copy Markdown
Author

I'm getting pretty close to having all the tests working, but for now I've decided to only support this feature for redis. It seems like the other backends require that all tasks be submitted immediately to allow the chord_unlock task to work (though I still have more investigation to find out why that is).

Before I can finish this, I have three questions:

  1. Are there other backends like redis that do not use chord_unlock?

  2. is it safe to assume that all of the 'chord' attributes on tasks processed by group._apply_tasks() will have the same value? To put it another way, is it possible for the callbacks within a group to be heterogenous? It seems to me that this should never happen, because in the redis backend each task caries the chord callback forward, and the last to complete triggers it, so the callback executed would be effectively random if the callbacks were not identical for all tasks in the group. That said, I'm not sure what happens when a chord has another chord as a subtask. I'll have to try that out.

  3. why does group._freeze_unroll() use maybe_signature() to ensure that the tasks are actually signatures (as opposed to dicts or lists), while group._traverse_tasks() does not, even though both perform a similar recursive "unrolling" of a group's tasks? At least in the case of the chord.apply_async() entry point, _traverse_tasks is called before _freeze_unroll, so if the latter is expecting non-signatures, the former will error before they get there. Is there any reason not to call maybe_signature in both?

Generally it seems there are a lot of different ways to do the same thing in the celery code and few comments to explain whether the subtle differences are intentional or not.

Last note, maybe_signature can return a list, but as far as I can tell, just about everywhere that it is called would result in an error if that actually happened.

@chadrik

chadrik commented Feb 15, 2016

Copy link
Copy Markdown
Author

sorry, one more:

  1. what is the point of the last 3 arguments to group._prepared?
    def _prepared(self, tasks, partial_args, group_id, root_id, app, dict=dict,
                  CallableSignature=abstract.CallableSignature,
                  from_dict=Signature.from_dict):

There is nowhere within the celery code that uses these arguments, and no comment explaining this bizarre behavior. And in fact, the dict argument isn't even used by the function itself.

@thedrow

thedrow commented Feb 15, 2016

Copy link
Copy Markdown
Contributor

I'm not familiar enough with the project so I'm assigning this to @ask to answer your questions.

@codecov-io

Copy link
Copy Markdown

Current coverage is 95.59%

Merging #3043 into master will not affect coverage as of 4734267

@@            master   #3043   diff @@
======================================
  Files          230     231     +1
  Stmts        32625   32798   +173
  Branches      2254    2274    +20
  Methods          0       0       
======================================
+ Hit          31187   31354   +167
- Partial        223     228     +5
- Missed        1215    1216     +1

Review entire Coverage Diff as of 4734267


Uncovered Suggestions

  1. +0.05% via .../test_certificate.py#61...76
  2. +0.05% via ...ity/test_security.py#78...92
  3. +0.04% via ...pp/test_schedules.py#596...609
  4. See 7 more...

Powered by Codecov. Updated on successful CI builds.

@ask

ask commented Feb 26, 2016

Copy link
Copy Markdown
Contributor
  1. Are there other backends like redis that do not use chord_unlock?

only the cache backend

  1. is it safe to assume that all of the 'chord' attributes on tasks processed by group._apply_tasks() will >have the same value?

In the case of chord: yes
There's always the possibility that people are building custom chord like constructs,
which is something that I'd like to support, but not sure if there's any use case for
having tasks in the same group have different chord/group_id headers.
If it makes it easier, or it's a good optimization I'd say we can disallow it.

  1. why does group._freeze_unroll() use maybe_signature() to ensure that the tasks are actually >signatures (as opposed to dicts or lists), while group._traverse_tasks() does not, even though both >perform a similar recursive "unrolling" of a group's tasks?

I would guess this is because _traverse_tasks() is only used by __length_hint__,
and it doesn't matter if the object is a Signature or a dict, as long as the count is the same.
chord.apply_async calls length_hint, but it only cares about the count, using maybe_signature
would be expensive for no benefit as far as I can see.

Generally it seems there are a lot of different ways to do the same thing in the celery code and few >comments to explain whether the subtle differences are intentional or not.

Do you have any examples of this? In general comments related to the code in the function only,
not about when to use what (that would belong to the docstring).

Last note, maybe_signature can return a list, but as far as I can tell, just about everywhere that it is >called would result in an error if that actually happened.

I'd guess this is something leftover from when all signatures had a builtin task.

  1. what is the point of the last 3 arguments to group._prepared?

It's localizing globals that are used within the loop, as for every iteration Python needs to
do a LOAD_GLOBAL for these. dict is indeed not used, but it's not always easy to keep it up to date with the code.

Usually small savings, but it's used a lot in the worker inner loop (together with inlining), the end result
being able to process a thousand or so tasks more per/second.

Another reason for doing this is for threads, since when threads wake up during interpreter shutdown
some globals may have been garbage collected already and the LOAD_GLOBAL ends in a NameError

Comment thread celery/utils/functional.py Outdated
if self.__done:
return repr(self.__consumed)
else:
return '[{}]'.format(', '.join(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use {0} here, as position-less format is not supported by all 2.7 versions

@ask

ask commented Feb 26, 2016

Copy link
Copy Markdown
Contributor

Btw, I like your changes here. I'm not sure I have understood all of them yet as I just looked at it briefly, maybe you could add some comments to describe what's happening in regen?

Good idea putting the length of the chord into redis, that totally makes sense since it's the only result backend that actually needs it. I guess you can also remove _traverse_result and length_hint, since they are not used anymore.

I think one problem with using set_chord_size separately is that it complicates error handling, and this is maybe something that we should think about as currently everything depends on that operation actually succeeding, and there is no retry or anything.

@ask

ask commented Feb 26, 2016

Copy link
Copy Markdown
Contributor

I.e. If the set_chord_size fails we should probably retry, and on giving up it would need to set an error status for the chord?

@ask

ask commented Feb 26, 2016

Copy link
Copy Markdown
Contributor

Oh, RedisBackend.set already retries, but not sure what to do when giving up, maybe just raising a ConnectionError is good enough even though it already started n-1 tasks.

@chadrik

chadrik commented Mar 2, 2016

Copy link
Copy Markdown
Author

Hi @ask
I addressed your notes. And thanks for the answers. Unfortunately, celery no longer figures into my development plans, so I'm not going to be able to invest much more time in this, but I think this is a pretty killer feature so I'll try to answer whatever questions you have.

@ask

ask commented Jul 8, 2016

Copy link
Copy Markdown
Contributor

I tried to merge this into master, but I have been unable to get the tests to pass so far.

The work is in the canvas-generator branch, commit: 23abf35

Need to postpone this to 4.1 for now as need to continue with getting 4.0 release out.

@ask ask added this to the v4.1 milestone Jul 8, 2016

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @chadrik is it still possible for you to check this out?

@auvipy auvipy self-assigned this Dec 21, 2017
@chadrik

chadrik commented Dec 21, 2017

Copy link
Copy Markdown
Author

I have my hands pretty full with other side projects at the moment, and I don't actually need this feature any more as I've taken a different approach in my project, but it'd be cool to have that little "Contributor" tag next to my name when I post issues here, so.... I may be able to find some time to give it a go :)

@ask Any chance you can push the merge you did? It would save me some time.

@auvipy

auvipy commented Dec 21, 2017

Copy link
Copy Markdown
Member

I am going to make it work on master. but you will definitely get the credit

@chadrik

chadrik commented Dec 21, 2017 via email

Copy link
Copy Markdown
Author

@auvipy

auvipy commented Dec 21, 2017

Copy link
Copy Markdown
Member

re base seems problematic, so I am trying to implement it cleanly step by step with smaller chunks. could you review it #4459 here later? might need your suggestions about testing part

@auvipy auvipy modified the milestones: v4.2, v5.0.0 Jan 13, 2018
@auvipy auvipy modified the milestones: v5.0.0, 4.6 Sep 22, 2019
@auvipy

auvipy commented Dec 15, 2020

Copy link
Copy Markdown
Member

closing in favor of new patch

@auvipy auvipy closed this Dec 15, 2020
@auvipy auvipy modified the milestones: 4.6, Future Dec 15, 2020
@auvipy auvipy modified the milestones: Future, 5.1.0 Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants