-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#97 helpers for more flexible tasks progression #98
Open
Skrattoune
wants to merge
3
commits into
boxine:main
Choose a base branch
from
Skrattoune:patch-6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,14 @@ | |
import math | ||
import sys | ||
import time | ||
import random | ||
|
||
from bx_py_utils.iteration import chunk_iterable | ||
from huey import crontab | ||
from huey.contrib.djhuey import lock_task, periodic_task, task | ||
|
||
from huey_monitor.models import TaskModel | ||
from huey_monitor.tqdm import ProcessInfo | ||
from huey_monitor.tqdm import ProcessInfo, make_task_complete | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -129,3 +130,66 @@ def parallel_task(task, total=2000, task_num=3, **info_kwargs): | |
logger.info('Start sub task no. %i', no) | ||
time.sleep(5) | ||
parallel_sub_task(parent_task_id=task.id, item_chunk=chunk, **info_kwargs) | ||
|
||
@task(context=True, retries=1) | ||
def sub_task_recursive(task, parent_task_id): | ||
""" | ||
Example of implementation for recursive tasks where final number of sub-tasks is unknown. | ||
Each recursive sub-task will refer to the same task as their parent task | ||
""" | ||
logger.info('Recursive sub task started from main task: %s', parent_task_id) | ||
TaskModel.objects.set_parent_task( | ||
main_task_id=parent_task_id, | ||
sub_task_id=task.id, | ||
) | ||
# let's consider we don't know yet the number of steps | ||
process_info = ProcessInfo(task, desc='Recursive task execution', total=999) | ||
|
||
continue_with_next_step = True | ||
|
||
while continue_with_next_step: | ||
# we execute the step: | ||
continue_with_next_step = random.randrange(100)<80 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just a normal for loop? e.g.:
|
||
|
||
# progress_count is incremented (default incrementation step is 1): | ||
process_info.update() | ||
# process_info.update(n=10) for incrementing by 10 | ||
|
||
# Update TaskModel.total based on TaskModel.progress_count : | ||
# (was 999 before because final number of steps was unknown) | ||
process_info.make_complete() | ||
|
||
# progress_count of the parent task is incremented (default incrementation step is 1): | ||
process_info.update_parent_progress() | ||
# process_info.update_parent_progress(n=5) for incrementing by 5 | ||
|
||
# for convenience purpose, the function 'huey_monitor.tqdm.update_task_progress(task_id, n=1)' | ||
# can also be called if 'process_info' is not declared in current code | ||
|
||
# we now test if conditions are met to exit the recursive loop: | ||
|
||
condition_for_recursive_loop_exit = random.randrange(10)>7 | ||
if condition_for_recursive_loop_exit: | ||
logger.info('This was the last of the recursive sub tasks') | ||
|
||
# Update TaskModel.total based on TaskModel.progress_count for the parent: | ||
# (was 999 before because final number of sub-tasks was unknown) | ||
make_task_complete(parent_task_id) | ||
else: | ||
# next recursive task is launched: | ||
sub_task_recursive(parent_task_id=parent_task_id) | ||
|
||
|
||
@task(context=True) | ||
def main_task_recursive(task): | ||
""" | ||
Example of implementation for recursive tasks where final number of sub-tasks is unknown. | ||
This is the parent task which launches the recursive search | ||
and will act as parent task for all sub-tasks | ||
""" | ||
logger.info('Main task %s starts recursive sub tasks', task.id) | ||
process_info = ProcessInfo(task, desc='Launching recursive tasks', total=999) | ||
# we don't know yet the number of sub-tasks | ||
|
||
sub_task_recursive(parent_task_id=task.id) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm.
total=999
is a little bit ugly, isn't it?Can we resolve this in a other way?