[SDESK-7371] Support eager mode in async celery #2745
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
The purpose of this PR is to modify the
HybridAppContextTask
class to support different configurations for Celery task execution, specifically ensuring compatibility with both eager and non-eager execution modes. The changes also aim to make apply_async() and delay() methods asynchronous to allow awaiting the scheduling of a task or either returning the result of the task directly if eager mode ison
.What has changed
CELERY_TASK_ALWAYS_EAGER
is True): Tasks are executed synchronously and awaited.CELERY_TASK_ALWAYS_EAGER
is False): If no loop is running, a new one is created; otherwise, the task is scheduled asynchronously.apply_async()
anddelay()
await task results in eager mode (no need to overridedelay
since it is essentially an alias method ofapply_async
)background_tasks
to manage references to tasks created withasyncio.create_task()
._is_always_eager()
utility function to determine eager mode.How to test
Here are two demo tasks that you can call either in eager and non-eager mode and observe the result and behaviour
then run them like
with eager mode ON, you should have something like this (notice that they run sync)
with eager mode OFF, should be like this below. Notice that
dump_task_2
is executed whiledump_task
sleeps for 5 seconds. Then finally dump_task finishes.Thanks for checking!
Resolves: SDESK-7371