-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
217 add functionality to send callback messages (#225)
* ProgressReporter class added * ProgressReporter.report_progress signature was changed * help string updated for report_progress * test_progress_report was added * report_progress refactored, extra test added * Update oda_api/api.py Co-authored-by: Denys Savchenko <[email protected]> * tests updated to address reviewer's comments * ProgressReporter documentation was added * bug fixing tests/test_progress_report.py --------- Co-authored-by: Denys Savchenko <[email protected]>
- Loading branch information
1 parent
f8ce41a
commit 961a316
Showing
4 changed files
with
189 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Progress reporting for long running tasks" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from oda_api.api import ProgressReporter\n", | ||
"import time" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Initialise ProgressReporter" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"pr = ProgressReporter()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Split your task into subtasks to enable progress report\n", | ||
"use ProgressReporter.report_progress method to send progress reports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"n_steps = 5\n", | ||
"for step in range(n_steps):\n", | ||
" stage = f\"simulation stage {step}\"\n", | ||
" progress = 100 * step // n_steps\n", | ||
" n_substeps = 10\n", | ||
" \n", | ||
" # optionally define subtasks\n", | ||
" for substep in range(n_substeps):\n", | ||
" substage = f\"subtask {substep}\"\n", | ||
" subtask_progress = 100 * substep // n_substeps\n", | ||
"\n", | ||
" time.sleep(0.001) # replace this by actual calculation\n", | ||
"\n", | ||
" # report progress, optionally adding extra message\n", | ||
" message='some message'\n", | ||
" \n", | ||
" pr.report_progress(stage=stage, progress=progress, substage=substage, subprogress=subtask_progress, message=message)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernel_info": { | ||
"name": "python2" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.18" | ||
}, | ||
"nteract": { | ||
"version": "0.15.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from oda_api.api import ProgressReporter | ||
import os | ||
import json | ||
|
||
callback_file = ".oda_api_callback" | ||
request_params = dict(stage='simulation', progress=50, substage='spectra', subprogress=30, message='some message') | ||
|
||
def test_progress_reporter_disabled(): | ||
if os.path.isfile(callback_file): | ||
os.remove(callback_file) | ||
# if callback is not available | ||
pr = ProgressReporter() | ||
assert not pr.enabled | ||
# the call below should not produce exception | ||
try: | ||
pr.report_progress(**request_params) | ||
except: | ||
assert False, 'report_progress raises exception in case of disabled ProgressReporter' | ||
|
||
def test_progress_reporter_enabled(): | ||
# if callback is available | ||
try: | ||
dump_file = 'callback' | ||
with open(callback_file, 'w') as file: | ||
print(f'file://{os.getcwd()}/{dump_file}', file=file) | ||
|
||
pr = ProgressReporter() | ||
assert pr.enabled | ||
|
||
pr.report_progress(**request_params) | ||
|
||
# verify that params passed to report_progress were saved to dump_file | ||
with open(dump_file) as json_file: | ||
saved_params = json.load(json_file) | ||
# append extra param befor check | ||
request_params['action'] = 'progress' # this key is added by report_progress | ||
assert saved_params == request_params | ||
finally: | ||
if os.path.isfile(callback_file): | ||
os.remove(callback_file) | ||
if os.path.isfile(dump_file): | ||
os.remove(dump_file) | ||
|