Wait for the future to complete and display the progress bar. This method may be used to drive any custom progress bar, which displays progress in percent from 0 to 100. Parameters ---------- fut: dask future future object for the batch of tasks submitted to the dis
(fut, progress_bar=None)
| 106 | |
| 107 | |
| 108 | def wait_and_display_progress(fut, progress_bar=None): |
| 109 | """ |
| 110 | Wait for the future to complete and display the progress bar. |
| 111 | This method may be used to drive any custom progress bar, which |
| 112 | displays progress in percent from 0 to 100. |
| 113 | |
| 114 | Parameters |
| 115 | ---------- |
| 116 | fut: dask future |
| 117 | future object for the batch of tasks submitted to the distributed |
| 118 | client. |
| 119 | progress_bar: callable or None |
| 120 | callable function or callable object with methods `start()`, |
| 121 | `__call__(float)` and `finish()`. The methods `start()` and |
| 122 | `finish()` are optional. For example, this could be a reference |
| 123 | to an instance of the object `TerminalProgressBar` |
| 124 | |
| 125 | Examples |
| 126 | -------- |
| 127 | |
| 128 | .. code-block:: |
| 129 | |
| 130 | client = Client() |
| 131 | data = da.random.random(size=(100, 100), chunks=(10, 10)) |
| 132 | sm_fut = da.sum(data, axis=0).persist(scheduler=client) |
| 133 | |
| 134 | # Call the progress monitor |
| 135 | wait_and_display_progress(sm_fut, TerminalProgressBar("Monitoring progress: ")) |
| 136 | |
| 137 | sm = sm_fut.compute(scheduler=client) |
| 138 | client.close() |
| 139 | """ |
| 140 | |
| 141 | # If there is no progress bar, then just return without waiting for the future |
| 142 | if progress_bar is None: |
| 143 | return |
| 144 | |
| 145 | if hasattr(progress_bar, "start"): |
| 146 | progress_bar.start() |
| 147 | |
| 148 | progress_bar(1.0) |
| 149 | while True: |
| 150 | done, not_done = wait(fut, return_when="FIRST_COMPLETED") |
| 151 | n_completed, n_pending = len(done), len(not_done) |
| 152 | n_total = n_completed + n_pending |
| 153 | percent_completed = n_completed / n_total * 100.0 if n_total > 0 else 100.0 |
| 154 | |
| 155 | # It is guaranteed that 'progress_bar' is called for 100% completion |
| 156 | progress_bar(percent_completed) |
| 157 | |
| 158 | if not n_pending: |
| 159 | break |
| 160 | ttime.sleep(0.5) |
| 161 | |
| 162 | if hasattr(progress_bar, "finish"): |
| 163 | progress_bar.finish() |
| 164 | |
| 165 |