You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
group and chord: subtasks are not executed until entire generator is consumed #3021
When passing a generator to group or chord, sub-tasks are not submitted to the broker for execution until the entire generator is consumed. The expected result is that subtasks are submitted during iteration, as soon as they are visited.
@chadrik Your example is really strange. Why do you need a group when you expect synchronous behaviour?
Anyway, I think you are wrong. First you create a signature of a task which you call later which in fact is a group which consists of add subtasks. It is created yielding from a logyield in a for loop.
Later after the group is created and saved to g you call it and add subtasks are executed in parallel.
What would work as you want is this:
result = [add.delay(i, i).get() for i in longyield()]
print result
I used group.delay() but that doesn't make a difference. By extending the time it takes to yield results (e.g. longyield vs xrange) I'm trying to make apparent that group is consuming the entire generator before submitting subtasks for execution.
Why do you need a group when you expect synchronous behaviour?
I don't understand your question. I want all sub-tasks executed in parallel. I thought that was the point of the group primitive.
I'm also not the only person who has had this problem. I found this old reference from the celery-users google group, but the suggestions from Ask seemed out of date for the 3.1.x codebase, and the ones that I tried did not help. I was surprised to find this issue has not been address in 4.x.
Yes, that's the workaround I'm using for now, but the point is that I want to use the canvas primitives so that I can combine them into complex topologies.
@syradium I got so annoyed trying to prove to you that you don't know what you're talking about that I just went ahead and fixed the problem.
The issue is that there's a test for empty tasks which forces the entire generator to be consumed before it's really needed (note the line if not self.tasks):
File "/Users/chad/python/celery_walk_task.py", line 79, in itertest
result = g()
File "/Users/chad/dev/celery4.venv/lib/python2.7/site-packages/celery/canvas.py", line 807, in __call__
return self.apply_async(partial_args, **options)
File "/Users/chad/dev/celery4.venv/lib/python2.7/site-packages/celery/canvas.py", line 760, in apply_async
if not self.tasks:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserList.py", line 30, in __len__
def __len__(self): return len(self.data)
File "/Users/chad/dev/celery4.venv/lib/python2.7/site-packages/celery/utils/functional.py", line 353, in data
self.__consumed.extend(list(self.__it))
File "/Users/chad/python/celery_walk_task.py", line 77, in <genexpr>
g = group(add.s(i, i) for i in longyield())
File "/Users/chad/python/celery_walk_task.py", line 68, in longyield
raise
The tasks are held in a _regen class, which caches the generator so that it can be consumed more than once, like a list (it is in fact a sublcass of UserList and list). So, to get around the expansion of the generator just for this nonzero test, I added an optimized __nonzero__ method:
def__nonzero__(self):
iflen(self.__consumed):
returnTruetry:
# consume just one itemself.__consumed.append(next(self.__it))
exceptStopIteration:
returnFalseelse:
returnTrue
By adding this, I was able to get past the nonzero test without consuming the entire generator, and the rest of the code handled the generator appropriately, submitting each task for async execution as it was yielded from the generator.
I'm going to test this out a bit and submit a pull request. Thanks for the motivation.
I understand what are you talking about but wasn't sure we are on the same page. To me these two lines were identical (in celery terms):
group(add.s(i, i) for i in range(10))
group([add.s(i, i) for i in range(10)])
Be default I assumed that celery builds a list of whatever you give to it. Because in order to properly schedule a group of tasks it needs the whole group signature and not a generator which in fact can be infinite have side-effects and so on.
I do not see a use case for what you want to introduce. If each generator iteration is a long task and you want to start task in group as soon as possible, then why not to move this out to another task, make chain/chord, etc ... I am not sure if this is absolutely safe. Will it correctly work with get(propagate=...) if an exception is raised during yielding.
Anyway, that's just my opinion. If community agree on that then ok.
I think I understand the issue because I think it's one I've encountered. I'm a celery newbie so hopefully I won't mangle any terms too badly.
Let's say you have task A that generates chord B with callback C. A retrieves a few million rows from a database, does something to chunks of them in B and then merges the results in C. You're expecting the results from A to be very large but the results from B should be much smaller.
It would be ideal not to force A to keep its data around until B has been created. If there were a way for group/chord to start executing its tasks immediately, task A would only need to remember the GID of its subtasks (which could each be working on large chunks of data) instead of its entire data set. If A takes a long time to run, you could be losing the possibility of having a large amount of the work done by the time you finish generating B.
This would require some kind of call to mark B as "fully submitted for processing". From what I've seen of Redis as a broker (the only one I've used so far), it's using the native increment/decrement functions to check when the task is done. In this case you'd need to use those functions but also check that there wasn't potentially new work incoming for that chord. Just because I've completed 5/5 subtasks doesn't meant that A is done submitting tasks unless it says so.
I've explored this a bit more deeply and I've modified the celery code to work as desired for simple groups and it's almost there for chords.
The way that chords in celery+redis work under the hood is this:
the number of sub-tasks in the group is computed and stored on the chord
the chord is stored on each group subtask
in the redis backend, when each subtask in the group completes, the number of completed subtasks is checked. if the number complete equals the computed group size, then the chord callback is executed (except not quite. there's a little wrinkle which I'll explain later).
To put it another way, each group subtask carries a copy of the callback subtask forward, but only one -- the last to complete -- will execute it.
And, of course, the first step described above, computing the number of tasks, necessarily means consuming the complete generator of subtasks, which is what I'm hoping to avoid.
Looking at this, it doesn't seem that it would be that difficult to add a new group key along the lines of .t or .j which holds the total number of tasks, which is null (-1) until set by A when it submits the final task from group B.
The only thing I don't understand yet is the totaldiff offset, e.g.
This issue is currently causing me a lot of problems also.
I see that @chadrik's PR (#3043) has unfortunately not landed as of yet and he is no longer working on it at this point. I don't suppose any of the maintainers are able to pick up @chadrik's PR and get it ready for merging? I'm happy to help out with any testing, however, I'm not in a position to complete his work myself, unfortunately.
My use-case
I have three tasks:
A: Periodic task for the sending of weekly messages to customers
B: Task which does all the data crunching to generate each message and calls out to an external API to deliver the message
C: Callback for chord of task Bs to report on sending job
To me this seems like an obvious use-case for chord, however, the amount of memory used by task A in generating the chord header is in the order of gigabytes.
Reacted by tz, Flávio Juvenal and Kiron Krishnankutty
Activity
sysradium commentedon Jan 24, 2016
@chadrik Your example is really strange. Why do you need a group when you expect synchronous behaviour?
Anyway, I think you are wrong. First you create a signature of a task which you call later which in fact is a group which consists of
addsubtasks. It is createdyieldingfrom alogyieldin aforloop.Later after the group is created and saved to
gyou call it andaddsubtasks are executed in parallel.What would work as you want is this:
But looks pointless.
chadrik commentedon Jan 24, 2016
@syradium Hopefully my example is not pointless because it is taken almost directly from the celery docs:
I used
group.delay()but that doesn't make a difference. By extending the time it takes to yield results (e.g.longyieldvsxrange) I'm trying to make apparent thatgroupis consuming the entire generator before submitting subtasks for execution.I don't understand your question. I want all sub-tasks executed in parallel. I thought that was the point of the
groupprimitive.I'm also not the only person who has had this problem. I found this old reference from the celery-users google group, but the suggestions from Ask seemed out of date for the 3.1.x codebase, and the ones that I tried did not help. I was surprised to find this issue has not been address in 4.x.
Yes, that's the workaround I'm using for now, but the point is that I want to use the canvas primitives so that I can combine them into complex topologies.
sysradium commentedon Jan 24, 2016
@chadrik
Yes and docs are correct. Your example is a little bit different.
Look, this:
Is equivalent of:
Right?
add.s(0, 0)does not start/execute a task. It only creates a subtask. You can later call it with.delay()/apply_async():.. or by running a group. This is why your tasks start running only after your generator stops.
chadrik commentedon Jan 25, 2016
@syradium I got so annoyed trying to prove to you that you don't know what you're talking about that I just went ahead and fixed the problem.
The issue is that there's a test for empty tasks which forces the entire generator to be consumed before it's really needed (note the line
if not self.tasks):The tasks are held in a
_regenclass, which caches the generator so that it can be consumed more than once, like a list (it is in fact a sublcass ofUserListandlist). So, to get around the expansion of the generator just for this nonzero test, I added an optimized__nonzero__method:By adding this, I was able to get past the nonzero test without consuming the entire generator, and the rest of the code handled the generator appropriately, submitting each task for async execution as it was yielded from the generator.
I'm going to test this out a bit and submit a pull request. Thanks for the motivation.
sysradium commentedon Jan 25, 2016
@chadrik wow, take it easy.
I understand what are you talking about but wasn't sure we are on the same page. To me these two lines were identical (in celery terms):
Be default I assumed that
celerybuilds a list of whatever you give to it. Because in order to properly schedule a group of tasks it needs the whole group signature and not a generator which in fact can be infinite have side-effects and so on.I do not see a use case for what you want to introduce. If each generator iteration is a long task and you want to start task in group as soon as possible, then why not to move this out to another task, make chain/chord, etc ... I am not sure if this is absolutely safe. Will it correctly work with
get(propagate=...)if an exception is raised during yielding.Anyway, that's just my opinion. If community agree on that then ok.
detzgk commentedon Jan 29, 2016
I think I understand the issue because I think it's one I've encountered. I'm a celery newbie so hopefully I won't mangle any terms too badly.
Let's say you have task A that generates chord B with callback C. A retrieves a few million rows from a database, does something to chunks of them in B and then merges the results in C. You're expecting the results from A to be very large but the results from B should be much smaller.
It would be ideal not to force A to keep its data around until B has been created. If there were a way for group/chord to start executing its tasks immediately, task A would only need to remember the GID of its subtasks (which could each be working on large chunks of data) instead of its entire data set. If A takes a long time to run, you could be losing the possibility of having a large amount of the work done by the time you finish generating B.
This would require some kind of call to mark B as "fully submitted for processing". From what I've seen of Redis as a broker (the only one I've used so far), it's using the native increment/decrement functions to check when the task is done. In this case you'd need to use those functions but also check that there wasn't potentially new work incoming for that chord. Just because I've completed 5/5 subtasks doesn't meant that A is done submitting tasks unless it says so.
It's certainly not trivial to implement.
chadrik commentedon Jan 31, 2016
Yep, you've nailed it.
I've explored this a bit more deeply and I've modified the celery code to work as desired for simple groups and it's almost there for chords.
The way that chords in celery+redis work under the hood is this:
To put it another way, each group subtask carries a copy of the callback subtask forward, but only one -- the last to complete -- will execute it.
And, of course, the first step described above, computing the number of tasks, necessarily means consuming the complete generator of subtasks, which is what I'm hoping to avoid.
The relevant backend code is here:
Looking at this, it doesn't seem that it would be that difficult to add a new group key along the lines of
.tor.jwhich holds the total number of tasks, which is null (-1) until set by A when it submits the final task from group B.The only thing I don't understand yet is the
totaldiffoffset, e.g.Why can't we compare
readycountdirectly tocallback['chord_size']? Why the need for the offset? I'll have to do some more digging.chadrik commentedon Jan 31, 2016
Got it working without too much hassle. I'll post a pull request soon.
prydie commentedon May 27, 2016
This issue is currently causing me a lot of problems also.
I see that @chadrik's PR (#3043) has unfortunately not landed as of yet and he is no longer working on it at this point. I don't suppose any of the maintainers are able to pick up @chadrik's PR and get it ready for merging? I'm happy to help out with any testing, however, I'm not in a position to complete his work myself, unfortunately.
My use-case
I have three tasks:
A: Periodic task for the sending of weekly messages to customersB: Task which does all the data crunching to generate each message and calls out to an external API to deliver the messageC: Callback forchordof taskBs to report on sending jobTo me this seems like an obvious use-case for
chord, however, the amount of memory used by taskAin generating the chord header is in the order of gigabytes.thedrow commentedon Jul 11, 2017
We'll try to get this on 4.1.
RyanNaughton commentedon Sep 15, 2017
Hi, any update on this issue?
thedrow commentedon Sep 16, 2017
No unfortunately none of us had time to work on this so far.
4 remaining items
thedrow commentedon Mar 4, 2021
This is finally fixed and will appear in 5.1.
chadrik commentedon Mar 4, 2021
RyanNaughton commentedon Mar 4, 2021
Awesome!
@pigrabbit87 Remember how this always used mess up our jobs! Flash from the past!
thedrow commentedon Mar 10, 2021
I'm sorry to hear that it happened :) It must have been frustrating.