added optional info line

This commit is contained in:
cimatosa 2015-06-26 17:01:05 +02:00
parent d5a4c8ea9f
commit 0fa9244cda
3 changed files with 88 additions and 36 deletions

View file

@ -1069,18 +1069,22 @@ class JobManager_Server(object):
else: else:
Progress = progress.ProgressSilentDummy Progress = progress.ProgressSilentDummy
info_line = progress.StringValue(num_of_bytes=80)
with Progress(count = self._numresults, with Progress(count = self._numresults,
max_count = self._numjobs, max_count = self._numjobs,
interval = self.msg_interval, interval = self.msg_interval,
speed_calc_cycles=self.speed_calc_cycles, speed_calc_cycles = self.speed_calc_cycles,
verbose = self.verbose, verbose = self.verbose,
sigint='ign', sigint = 'ign',
sigterm='ign') as stat: sigterm = 'ign',
info_line = info_line) as stat:
stat.start() stat.start()
while (len(self.args_set) - self.fail_q.qsize()) > 0: while (len(self.args_set) - self.fail_q.qsize()) > 0:
try: try:
info_line.value = "result_q size: {}".format(self.result_q.qsize()).encode('utf-8')
arg, result = self.result_q.get(timeout=1) arg, result = self.result_q.get(timeout=1)
self.args_set.remove(arg) self.args_set.remove(arg)
self.numresults = self.numjobs - len(self.args_set) self.numresults = self.numjobs - len(self.args_set)

View file

@ -332,15 +332,16 @@ class Progress(Loop):
""" """
def __init__(self, def __init__(self,
count, count,
max_count=None, max_count = None,
prepend=None, prepend = None,
width='auto', width = 'auto',
speed_calc_cycles=10, speed_calc_cycles = 10,
interval=1, interval = 1,
verbose=0, verbose = 0,
sigint='stop', sigint = 'stop',
sigterm='stop', sigterm = 'stop',
name='progress'): name = 'progress',
info_line = None):
""" """
count [mp.Value] - shared memory to hold the current state, (list or single value) count [mp.Value] - shared memory to hold the current state, (list or single value)
@ -382,7 +383,7 @@ class Progress(Loop):
except TypeError: except TypeError:
raise TypeError("'max_count' must be iterable") raise TypeError("'max_count' must be iterable")
else: else:
assert isinstance(max_count, mp.sharedctypes.Synchronized), "'max_count' must be if the type multiprocessing.sharedctypes.Synchronized" assert isinstance(max_count, mp.sharedctypes.Synchronized), "'max_count' must be of the type multiprocessing.sharedctypes.Synchronized"
max_count = [max_count] max_count = [max_count]
else: else:
max_count = [None] * self.len max_count = [None] * self.len
@ -429,6 +430,8 @@ class Progress(Loop):
self.show_on_exit = False self.show_on_exit = False
self.add_args = {} self.add_args = {}
self.info_line = info_line
# setup loop class with func # setup loop class with func
super(Progress, self).__init__(func=Progress.show_stat_wrapper_multi, super(Progress, self).__init__(func=Progress.show_stat_wrapper_multi,
args=(self.count, args=(self.count,
@ -444,7 +447,8 @@ class Progress(Loop):
self.__class__.show_stat, self.__class__.show_stat,
self.len, self.len,
self.add_args, self.add_args,
self.lock), self.lock,
self.info_line),
interval=interval, interval=interval,
verbose=verbose, verbose=verbose,
sigint=sigint, sigint=sigint,
@ -551,7 +555,9 @@ class Progress(Loop):
self.__class__.show_stat, self.__class__.show_stat,
self.len, self.len,
self.add_args, self.add_args,
self.lock) self.lock,
self.info_line,
no_move_up=True)
def reset(self, i = None): def reset(self, i = None):
""" """
@ -629,7 +635,9 @@ class Progress(Loop):
show_stat_function, show_stat_function,
len_, len_,
add_args, add_args,
lock): lock,
info_line,
no_move_up=False):
""" """
call the static method show_stat_wrapper for each process call the static method show_stat_wrapper for each process
""" """
@ -650,7 +658,22 @@ class Progress(Loop):
add_args, add_args,
i, i,
lock[i]) lock[i])
print(ESC_MOVE_LINE_UP(len_) + ESC_NO_CHAR_ATTR, end='') n = len_
if info_line is not None:
s = info_line.value.decode('utf-8')
s = s.split('\n')
n += len(s)
for si in s:
if width == 'auto':
width = get_terminal_width()
if len(si) > width:
si = si[:width]
print("{0:<{1}}".format(si, width))
if no_move_up:
n = 0
print(ESC_MOVE_LINE_UP(n) + ESC_NO_CHAR_ATTR, end='')
sys.stdout.flush() sys.stdout.flush()
def start(self): def start(self):
@ -683,7 +706,7 @@ class Progress(Loop):
if self.show_on_exit: if self.show_on_exit:
self._show_stat() self._show_stat()
print('\n'*(self.len-1)) print()
self.show_on_exit = False self.show_on_exit = False
@ -701,7 +724,8 @@ class ProgressBar(Progress):
verbose=0, verbose=0,
sigint='stop', sigint='stop',
sigterm='stop', sigterm='stop',
name='progress_bar'): name='progress_bar',
info_line=None):
""" """
width [int/'auto'] - the number of characters used to show the Progress bar, width [int/'auto'] - the number of characters used to show the Progress bar,
use 'auto' to determine width from terminal information -> see _set_width use 'auto' to determine width from terminal information -> see _set_width
@ -715,7 +739,8 @@ class ProgressBar(Progress):
verbose = verbose, verbose = verbose,
sigint=sigint, sigint=sigint,
sigterm=sigterm, sigterm=sigterm,
name=name) name=name,
info_line=info_line)
self._PRE_PREPEND = ESC_NO_CHAR_ATTR + ESC_RED self._PRE_PREPEND = ESC_NO_CHAR_ATTR + ESC_RED
self._POST_PREPEND = ESC_BOLD + ESC_GREEN self._POST_PREPEND = ESC_BOLD + ESC_GREEN
@ -782,7 +807,8 @@ class ProgressBarCounter(Progress):
verbose=0, verbose=0,
sigint='stop', sigint='stop',
sigterm='stop', sigterm='stop',
name='progress_bar_counter'): name='progress_bar_counter',
info_line=None):
super(ProgressBarCounter, self).__init__(count=count, super(ProgressBarCounter, self).__init__(count=count,
max_count=max_count, max_count=max_count,
@ -793,7 +819,8 @@ class ProgressBarCounter(Progress):
verbose = verbose, verbose = verbose,
sigint=sigint, sigint=sigint,
sigterm=sigterm, sigterm=sigterm,
name=name) name=name,
info_line=info_line)
self.counter_count = [] self.counter_count = []
self.counter_q = [] self.counter_q = []
@ -889,7 +916,8 @@ class ProgressBarFancy(Progress):
verbose=0, verbose=0,
sigint='stop', sigint='stop',
sigterm='stop', sigterm='stop',
name='progress_bar'): name='progress_bar',
info_line=None):
""" """
width [int/'auto'] - the number of characters used to show the Progress bar, width [int/'auto'] - the number of characters used to show the Progress bar,
use 'auto' to determine width from terminal information -> see _set_width use 'auto' to determine width from terminal information -> see _set_width
@ -906,7 +934,8 @@ class ProgressBarFancy(Progress):
verbose = verbose, verbose = verbose,
sigint=sigint, sigint=sigint,
sigterm=sigterm, sigterm=sigterm,
name=name) name=name,
info_line=info_line)
@staticmethod @staticmethod
def get_d(s1, s2, width, lp, lps): def get_d(s1, s2, width, lp, lps):
@ -1180,6 +1209,9 @@ def FloatValue(val=0.):
def UnsignedIntValue(val=0): def UnsignedIntValue(val=0):
return mp.Value('I', val, lock=True) return mp.Value('I', val, lock=True)
def StringValue(num_of_bytes):
return mp.Array('c', bytearray(num_of_bytes), lock=True)
def check_process_termination(proc, identifier, timeout, verbose=0, auto_kill_on_last_resort = False): def check_process_termination(proc, identifier, timeout, verbose=0, auto_kill_on_last_resort = False):
proc.join(timeout) proc.join(timeout)

View file

@ -732,6 +732,21 @@ def test_progress_bar_counter_fancy_hide_bar():
if (time.time() - t0) > 15: if (time.time() - t0) > 15:
break break
def test_info_line():
c1 = progress.UnsignedIntValue(val=0)
s = progress.StringValue(80)
m1 = progress.UnsignedIntValue(val=30)
with progress.ProgressBarFancy(count=c1, max_count=m1, verbose=1, interval=0.2, info_line=s) as sc:
sc.start()
while True:
c1.value = c1.value + 1
if c1.value > 10:
s.value = b'info_line\nline2'
time.sleep(0.1)
if c1.value >= m1.value:
break
if __name__ == "__main__": if __name__ == "__main__":
func = [ func = [
# test_loop_basic, # test_loop_basic,
@ -752,12 +767,13 @@ if __name__ == "__main__":
# test_progress_bar_counter_hide_bar, # test_progress_bar_counter_hide_bar,
# test_progress_bar_slow_change, # test_progress_bar_slow_change,
# test_progress_bar_start_stop, # test_progress_bar_start_stop,
test_progress_bar_fancy, # test_progress_bar_fancy,
test_progress_bar_multi_fancy, # test_progress_bar_multi_fancy,
test_progress_bar_fancy_small, # test_progress_bar_fancy_small,
test_progress_bar_counter_fancy, # test_progress_bar_counter_fancy,
test_progress_bar_counter_fancy_non_max, # test_progress_bar_counter_fancy_non_max,
test_progress_bar_counter_fancy_hide_bar, # test_progress_bar_counter_fancy_hide_bar,
test_info_line,
lambda: print("END") lambda: print("END")
] ]