|
| 1 | +# Derived from `distributed.diagnostics.progressbar` which is |
| 2 | + |
| 3 | +# Copyright (c) 2015-2017, Anaconda, Inc. and contributors |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions |
| 8 | +# are met: |
| 9 | +# |
| 10 | +# Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# |
| 13 | +# Redistributions in binary form must reproduce the above copyright |
| 14 | +# notice, this list of conditions and the following disclaimer in the |
| 15 | +# documentation and/or other materials provided with the distribution. |
| 16 | +# |
| 17 | +# Neither the name of Anaconda nor the names of any contributors may |
| 18 | +# be used to endorse or promote products derived from this software |
| 19 | +# without specific prior written permission. |
| 20 | +# |
| 21 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 31 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | +# POSSIBILITY OF SUCH DAMAGE. |
| 33 | + |
| 34 | + |
| 35 | +from contextlib import contextmanager |
| 36 | + |
| 37 | +from distributed.diagnostics.progressbar import ProgressBar |
| 38 | +from distributed.utils import LoopRunner |
| 39 | + |
| 40 | +from tornado.ioloop import IOLoop |
| 41 | + |
| 42 | +import sys |
| 43 | + |
| 44 | + |
| 45 | +def format_time(t): |
| 46 | + "Format seconds into a human readable form." |
| 47 | + m, s = divmod(t, 60) |
| 48 | + h, m = divmod(m, 60) |
| 49 | + return f'{int(h):02d}:{int(m):02d}:{int(s):02d}' |
| 50 | + |
| 51 | + |
| 52 | +class WebSocketProgressBar(ProgressBar): |
| 53 | + def __init__(self, keys, update_callback, scheduler=None, interval=1, |
| 54 | + loop=None, complete=True, start=True): |
| 55 | + super(WebSocketProgressBar, self).__init__(keys, scheduler, interval, |
| 56 | + complete) |
| 57 | + self.update_callback = update_callback |
| 58 | + self.loop = loop or IOLoop.current() |
| 59 | + |
| 60 | + if start: |
| 61 | + self.loop.add_callback(self.listen) |
| 62 | + |
| 63 | + def _draw_bar(self, remaining, all, **kwargs): |
| 64 | + frac = (1 - remaining / all) if all else 1.0 |
| 65 | + percent = frac * 100 |
| 66 | + elapsed = format_time(self.elapsed) |
| 67 | + self.update_callback({'percent': f'{percent:2.1f}', 'elapsed': elapsed}) |
0 commit comments