Last active
November 30, 2019 07:31
-
-
Save moskytw/894acaa47f727c8a735b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
'''Output: | |
$ py fun_with_progress.py | |
Start doing things without bar ... | |
Took: 0:00:13.598943 | |
Start doing things with bar ... | |
|████████████████████████████████| 10000/10000 [100.00%] [ETA: 0:00:00] | |
Took: 0:00:14.784131 | |
Start doing things with async bar ... | |
|████████████████████████████████| 10000/10000 [100.00%] [ETA: 0:00:00] | |
Took: 0:00:13.684287 | |
''' | |
from time import sleep | |
from datetime import datetime | |
from threading import Thread | |
from Queue import Queue | |
from progress.bar import Bar, IncrementalBar | |
# Name Value | |
# index current value | |
# max maximum value | |
# remaining max - index | |
# progress index / max | |
# percent progress * 100 | |
# avg simple moving average time per item (in seconds) | |
# elapsed elapsed time in seconds | |
# elapsed_td elapsed as a timedelta (useful for printing as a string) | |
# eta avg * remaining | |
# eta_td eta as a timedelta (useful for printing as a string) | |
# | |
# --https://github.com/verigak/progress/ | |
N = 10000 | |
#N = 10 | |
SEC_TO_SLEEP = 0.001 | |
SUFFIX = '%(index)d/%(max)d [%(percent).2f%%] [ETA: %(eta_td)s]' | |
def do_without_bar(): | |
print 'Start doing things without bar ...' | |
now_dt = datetime.now() | |
for i in xrange(N): | |
sleep(SEC_TO_SLEEP) | |
print 'Took: {}'.format(datetime.now()-now_dt) | |
def do_with_bar(): | |
print 'Start doing things with bar ...' | |
now_dt = datetime.now() | |
bar = IncrementalBar(max=N, suffix=SUFFIX) | |
for i in xrange(N): | |
sleep(SEC_TO_SLEEP) | |
bar.next() | |
bar.finish() | |
print 'Took: {}'.format(datetime.now()-now_dt) | |
class AsyncBar(Bar): | |
def __init__(self, *args, **arg_d): | |
self._f_que = Queue() | |
def consume(): | |
while True: | |
f = self._f_que.get() | |
if f is None: return | |
f() | |
self._f_que_consumer = Thread(target=consume) | |
self._f_que_consumer.daemon = True | |
self._f_que_consumer.start() | |
Bar.__init__(self, *args, **arg_d) | |
_next = Bar.next | |
def next(self, n=1): | |
self._f_que.put(lambda: self._next(n)) | |
_finish = Bar.finish | |
def finish(self): | |
self._f_que.put(lambda: self._finish()) | |
self._f_que.put(None) | |
self._f_que_consumer.join() | |
class AsyncIncrementalBar(AsyncBar, IncrementalBar): | |
pass | |
def do_with_async_bar(): | |
print 'Start doing things with async bar ...' | |
now_dt = datetime.now() | |
bar = AsyncIncrementalBar(max=N, suffix=SUFFIX) | |
for i in xrange(N): | |
sleep(SEC_TO_SLEEP) | |
bar.next() | |
bar.finish() | |
print 'Took: {}'.format(datetime.now()-now_dt) | |
if __name__ == '__main__': | |
do_without_bar() | |
do_with_bar() | |
do_with_async_bar() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment