Skip to content
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

Update tqdm.py #44

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
20 changes: 13 additions & 7 deletions huey_monitor/tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down