|
| 1 | +--- |
| 2 | +title: Progress |
| 3 | +description: Add progress indicators to provide visibility into the execution of a job |
| 4 | +icon: bars-progress |
| 5 | +tag: NEW |
| 6 | +--- |
| 7 | + |
| 8 | +Tilebox supports user-defined progress indicators during the execution of a job. This can be useful to provide visibility into the execution and the expected duration of a job, especially for longer running jobs. |
| 9 | + |
| 10 | +<Frame> |
| 11 | + <img src="/assets/workflows/progress/progress-light.png" alt="Tilebox Workflows progress indicators" className="dark:hidden" /> |
| 12 | + <img src="/assets/workflows/progress/progress-dark.png" alt="Tilebox Workflows progress indicators" className="hidden dark:block" /> |
| 13 | +</Frame> |
| 14 | + |
| 15 | + |
| 16 | +## Tracking Progress |
| 17 | + |
| 18 | +Progress indicators in Tilebox use a `done` / `total` model. Tasks can increase a `total` value to specify the total work to be done, and the same or any other task can increase a `done` counter to track the amount of work that has already been completed. |
| 19 | + |
| 20 | +Progress tracking is always done at a task level. Each task can report its progress updates, as increases in `done` and `total` independently, and the job's total progress is the sum of all tasks' progress. |
| 21 | + |
| 22 | +<Info> |
| 23 | + Progress tracking is currently only available in the Tilebox Python SDK. Go support is coming soon. |
| 24 | +</Info> |
| 25 | + |
| 26 | +<CodeGroup> |
| 27 | +```python Python |
| 28 | +from tilebox.workflows import Task, ExecutionContext |
| 29 | + |
| 30 | +class MyTask(Task): |
| 31 | + def execute(self, context: ExecutionContext) -> None: |
| 32 | + # report that 10 units of work need to be done |
| 33 | + context.progress().add(10) |
| 34 | + |
| 35 | + for _ in range(10): |
| 36 | + context.submit_subtask(MySubTask()) |
| 37 | + |
| 38 | +class MySubTask(Task): |
| 39 | + def execute(self, context: ExecutionContext) -> None: |
| 40 | + # report that one unit of work has been completed |
| 41 | + context.progress().done(1) |
| 42 | +``` |
| 43 | +</CodeGroup> |
| 44 | + |
| 45 | +## Multiple Progress Indicators |
| 46 | + |
| 47 | +A job can have multiple independent progress indicators. This is useful when a job consists of multiple steps, that each benefits from having its own progress indicator. |
| 48 | +To create a new progress indicator, call `context.progress(name)` with a unique `name` for the indicator. |
| 49 | + |
| 50 | +<CodeGroup> |
| 51 | +```python Python lines focus={14-15,35,51} |
| 52 | +from io import BytesIO |
| 53 | + |
| 54 | +import httpx # pip install httpx |
| 55 | +from PIL import Image # pip install pillow |
| 56 | +from tilebox.workflows import Task, ExecutionContext |
| 57 | + |
| 58 | +class DownloadImages(Task): |
| 59 | + image_urls: list[str] |
| 60 | + |
| 61 | + def execute(self, context: ExecutionContext) -> None: |
| 62 | + # download and process images from a list of URLs |
| 63 | + n = len(self.image_urls) |
| 64 | + |
| 65 | + context.progress("download").add(n) |
| 66 | + context.progress("process").add(n) |
| 67 | + |
| 68 | + for i, url in enumerate(self.image_urls): |
| 69 | + image_name = f"image_{i:04d}.png" |
| 70 | + grayscale_name = f"image_gray_{i:04d}.png" |
| 71 | + download = context.submit_subtask(DownloadImage(url, image_name)) |
| 72 | + process = context.submit_subtask( |
| 73 | + ToGrayscale(image_name, grayscale_name), |
| 74 | + depends_on=[download], |
| 75 | + ) |
| 76 | + |
| 77 | +class DownloadImage(Task): |
| 78 | + url: str |
| 79 | + image_name: str |
| 80 | + |
| 81 | + def execute(self, context: ExecutionContext) -> None: |
| 82 | + response = httpx.get(self.url, follow_redirects=True) |
| 83 | + context.job_cache[self.image_name] = response.read() |
| 84 | + |
| 85 | + # report that one image has been downloaded |
| 86 | + context.progress("download").done(1) |
| 87 | + |
| 88 | + |
| 89 | +class ToGrayscale(Task): |
| 90 | + input_image: str |
| 91 | + output_name: str |
| 92 | + |
| 93 | + def execute(self, context: ExecutionContext) -> None: |
| 94 | + image = Image.open(BytesIO(context.job_cache[self.input_image])) |
| 95 | + image = image.convert("L") # convert the image to grayscale |
| 96 | + |
| 97 | + buffer = BytesIO() |
| 98 | + image.save(buffer, format="png") |
| 99 | + |
| 100 | + context.job_cache[self.output_name] = buffer.getvalue() |
| 101 | + |
| 102 | + context.progress("process").done(1) |
| 103 | +``` |
| 104 | +</CodeGroup> |
| 105 | + |
| 106 | +## Querying Progress |
| 107 | + |
| 108 | +At any time during a job's execution, you can query the current progress of a job using the `find` method on the job client. The returned job object contains a `progress` field that contains the current progress of the job. |
| 109 | + |
| 110 | +<CodeGroup> |
| 111 | +```python |
| 112 | +from tilebox.workflows import Client |
| 113 | + |
| 114 | +job_client = Client().jobs() |
| 115 | +job = job_client.submit("download-images", DownloadImages( |
| 116 | + [ |
| 117 | + "https://picsum.photos/id/123/500/500", |
| 118 | + "https://picsum.photos/id/155/500/500", |
| 119 | + ], |
| 120 | +)) |
| 121 | + |
| 122 | +job = job_client.find(job.id) # refresh the job object |
| 123 | +print(job) |
| 124 | +``` |
| 125 | +</CodeGroup> |
| 126 | + |
| 127 | +```plaintext Output |
| 128 | +Job( |
| 129 | + id=UUID('019952b8-a5dc-f4c0-e428-724ccc587d83'), |
| 130 | + name='download-images', |
| 131 | + ..., |
| 132 | + progress=[ |
| 133 | + ProgressIndicator(label='download', total=2, done=1), |
| 134 | + ProgressIndicator(label='process', total=2, done=0), |
| 135 | + ] |
| 136 | +) |
| 137 | +``` |
| 138 | + |
| 139 | +## Progress idempotency |
| 140 | + |
| 141 | +Since tasks may fail and can subsequently be retried, it's possible that a task is executed more than once. This means that a task may report progress more than once. |
| 142 | +To avoid double-counting such progress updates, Tilebox only considers the progress reported by the last execution of a task. |
0 commit comments