python - Fabric counter to display how many host are done -
i'm trying create counter able display "host 5/100 done" @ end of task. have counter classe :
class threadcounter(object): def __init__(self, initval=0): self.val =int(initval) self.lock = threading.lock() def increment(self): self.lock: self.val += 1 def value(self): self.lock: return self.val
i tried few things make work : in main function, call task giving counter argument
def main(): my_counter = threadcounter(0) execute(the_task,my_counter) @parallel def the_task(counter): try: do---my--stuff : my_counter.increment() print my_counter.value()
the counter @ "1" when displayed. feel counter wasn't shared.
i tried declare counter global
same result. last try giving lock
the_task
, in the_task
:
with lock : counter += 1 print counter
what should share counter between same fabric tasks ? i'm new threads in python. (i use python 2.6 (not willingly))
you can use decorator track task completion like,
total = 100 count = 0 def counter(f): def wrapped(*args, **kwargs): globals()["count"] += 1 ret = f(*args, **kwargs) print "host %(count)s/%(total)s done" % globals() return ret return wrapped @counter def first_task(counter): # do---my--stuff pass @counter def second_task(counter): # do---my--stuff pass
Comments
Post a Comment