-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.py
69 lines (57 loc) · 1.95 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# coding=utf-8
from Queue import Queue
import threading
import time
import sys
__author__ = 'Zephyre'
def func_carrier(obj, interval, estimate=False):
th = threading.Thread(target=lambda: obj.run())
start_ts = time.time()
tm_ref = []
q = Queue(maxsize=100)
def timer_cb(last_time=False):
if hasattr(obj, 'callback'):
obj.callback()
text = obj.get_msg()
q_last = {'progress': obj.progress, 'time': time.time()}
if q.full():
q.get()
q.put(q_last)
if len(q.queue) > 1:
head = q.queue[0]
tail = q.queue[-1]
speed = (tail['progress'] - head['progress']) / (tail['time'] - head['time'])
if speed > 0:
eta = (obj.tot - tail['progress']) / speed
if eta > 3600:
hour = int(eta) / 3600
m = int(eta) % 3600 / 60
eta_str = str.format('Estimation: {0}h{1}m', hour, m)
elif eta > 60:
m = int(eta) / 60
sec = int(eta) % 60
eta_str = str.format('Estimation: {0}m{1}s', m, sec)
else:
eta_str = str.format('Estimation: {0}s', int(eta))
text = str.format('{0} {1}', text, eta_str)
sys.stdout.write('\r' + ' ' * 160 + '\r' + text)
sys.stdout.flush()
if not last_time:
ts = time.time()
next_ts = start_ts + interval * (int((ts - start_ts) / interval) + 1)
tm = threading.Timer(next_ts - ts, timer_cb)
tm_ref[0] = tm
tm.start()
tm = threading.Timer(interval, timer_cb)
tm_ref.append(tm)
if hasattr(obj, 'init_proc'):
obj.init_proc()
th.start()
tm.start()
th.join()
if hasattr(obj, 'tear_down'):
obj.tear_down()
tm_ref[0].cancel()
timer_cb(last_time=True)
sys.stdout.write('\n\n')
sys.stdout.flush()