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 2 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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ 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

cumulate_w_parent_progress=False, # optional: wether or not the progress reported on the sub task will be cumulated also at the parent task level
formacube marked this conversation as resolved.
Show resolved Hide resolved
# by default cumulate_w_parent_progress=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 cumulate_w_parent_progress=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,
cumulate_w_parent_progress=True,
formacube marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Parameters
Expand All @@ -32,6 +33,7 @@ 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.
cumulate_w_parent_progress: bool, optional: option to cumulate progress to the parent task progress if parent_task_id is provided
"""
assert isinstance(task, Task), f'No task given: {task!r} (Hint: use "context=True")'
self.task = task
Expand All @@ -40,6 +42,7 @@ def __init__(self,
self.unit = unit
self.unit_divisor = unit_divisor
self.parent_task_id = parent_task_id
self.cumulate_w_parent_progress = cumulate_w_parent_progress

if len(self.desc) > 64:
# We call .update() that will not validate the data, so a overlong
Expand Down Expand Up @@ -80,13 +83,16 @@ 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.cumulate_w_parent_progress:
formacube marked this conversation as resolved.
Show resolved Hide resolved
objects.append(
TaskProgressModel(
task_id=self.parent_task_id,
progress_count=n,
create_dt=now
)
)
)


TaskProgressModel.objects.bulk_create(objects)

Expand Down