diff --git a/README.md b/README.md index f1e9394..6d87c43 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,45 @@ def foobar_task(task, list_to_process): It is also possible to divide the work to several tasks and collect information about the processing of main-/sub-tasks. +```python +@task(context=True) +def foobar_task(task, list_to_process): + process_info = ProcessInfo( + task, + desc='A description of this Job', + total=len(list_to_process) + unit=' items' + ) + + for item in list_to_process: + foobar_subtask(item, parent_task_id = task.id) + process_info.update(n=1) # add the information that one item was processed + +@huey.db_task(context=True) +def foobar_subtask(item, task=None, parent_task_id=None): + process_info = ProcessInfo( + task, + parent_task_id=parent_task_id, # to link progress reporting to the parent task + + cumulate2parents=False, + # optional parameter: wether or not the progress reported on the sub task + # will be cumulated also at the parent task level + # by default cumulate2parents=True + + desc='A description of this Job', + total=len(item) # task_length, # info on task size + unit=' sub-items' + ) + + # ...to process the {item}... + for sub_item in item: + # ...do something with the {sub_items}... + process_info.update(n=1) # add the information that one sub-item was processed + # n will be added to the sub-task progress + # n will be added to the main-task progress also, unless cumulate2parents=False is passed as a parameter + +``` + Working example can be found in the test app here: [huey_monitor_tests/test_app/tasks.py](https://github.com/boxine/django-huey-monitor/blob/master/huey_monitor_tests/test_app/tasks.py) diff --git a/huey_monitor/tqdm.py b/huey_monitor/tqdm.py index 60d4a3a..df39d59 100644 --- a/huey_monitor/tqdm.py +++ b/huey_monitor/tqdm.py @@ -21,7 +21,8 @@ def __init__(self, total=None, unit='it', unit_divisor=1000, - parent_task_id=None + parent_task_id=None, + cumulate2parents=True, ): """ Parameters @@ -32,6 +33,8 @@ def __init__(self, unit: str, optional: String that will be used to define the unit of each iteration unit_divisor: int, optional parent_task_id: int, optional: Huey Task ID if a parent Tasks exists. + cumulate2parents: bool, optional: option to cumulate progress to the parent task progress + Note: parent_task_id must be provided to cumulate progress to the parent task progress """ assert isinstance(task, Task), f'No task given: {task!r} (Hint: use "context=True")' self.task = task @@ -40,6 +43,7 @@ def __init__(self, self.unit = unit self.unit_divisor = unit_divisor self.parent_task_id = parent_task_id + self.cumulate2parents = cumulate2parents if len(self.desc) > 64: # We call .update() that will not validate the data, so a overlong @@ -80,13 +84,15 @@ def update(self, n=1): if self.parent_task_id: # Store information for main task, too: ids.append(self.parent_task_id) - objects.append( - TaskProgressModel( - task_id=self.parent_task_id, - progress_count=n, - create_dt=now + + if self.cumulate2parents: + objects.append( + TaskProgressModel( + task_id=self.parent_task_id, + progress_count=n, + create_dt=now + ) ) - ) TaskProgressModel.objects.bulk_create(objects)