-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtimer.py
182 lines (147 loc) · 3.99 KB
/
timer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import bpy
import traceback
from queue import Queue
from typing import Any
from .kclogger import logger
class Timer:
TimerQueue = Queue()
TimerQueue2 = Queue()
stoped = False
@classmethod
def put(cls, delegate: Any):
if cls.stoped:
return
cls.TimerQueue.put(delegate)
@classmethod
def put2(cls, delegate: Any):
if cls.stoped:
return
cls.TimerQueue2.put(delegate)
@classmethod
def executor(cls, t):
if type(t) in {list, tuple}:
t[0](*t[1:])
else:
t()
@classmethod
def stop_added(cls):
cls.stoped = True
@classmethod
def start_added(cls):
cls.stoped = False
@classmethod
def run1(cls):
return cls.run_ex(cls.TimerQueue)
@classmethod
def run2(cls):
return cls.run_ex(cls.TimerQueue2)
@classmethod
def run_ex(cls, queue: Queue):
while not queue.empty():
t = queue.get()
try:
cls.executor(t)
except Exception as e:
traceback.print_exc()
logger.error("%s: %s", type(e).__name__, e)
except KeyboardInterrupt:
...
return 0.016666666666666666
@classmethod
def clear(cls):
while not cls.TimerQueue.empty():
cls.TimerQueue.get()
while not cls.TimerQueue2.empty():
cls.TimerQueue2.get()
@classmethod
def wait_run(cls, func):
def wrap(*args, **kwargs):
q = Queue()
def wrap_job(q):
try:
res = func(*args, **kwargs)
q.put(res)
except Exception as e:
q.put(e)
cls.put((wrap_job, q))
res = q.get()
if isinstance(res, Exception):
raise res
return res
return wrap
@classmethod
def reg(cls):
bpy.app.timers.register(cls.run1, persistent=True)
bpy.app.timers.register(cls.run2, persistent=True)
@classmethod
def unreg(cls):
cls.clear()
try:
bpy.app.timers.unregister(cls.run1)
bpy.app.timers.unregister(cls.run2)
except Exception:
...
class WorkerFunc:
args = {}
def __init__(self) -> None:
self.args = self.__class__.args
def __call__(self, *args: Any, **kwargs: Any) -> Any:
pass
class Worker:
JOB_WORK = set()
JOB_CLEAR = Queue()
@staticmethod
def push_worker(func):
Worker.JOB_WORK.add(func)
@staticmethod
def push_clear(func):
Worker.JOB_CLEAR.put(func)
@staticmethod
def remove_worker(func):
Worker.JOB_WORK.discard(func)
@staticmethod
def worker():
for func in Worker.JOB_WORK:
try:
Worker.executor(func)
except Exception as e:
traceback.print_exc()
logger.error("%s: %s", type(e).__name__, e)
except KeyboardInterrupt:
...
return 1
@staticmethod
def executor(t):
if type(t) in {list, tuple}:
t[0](*t[1:])
else:
t()
@staticmethod
def reg():
bpy.app.timers.register(Worker.worker, persistent=True)
@staticmethod
def unreg():
try:
bpy.app.timers.unregister(Worker.worker)
except Exception:
...
@staticmethod
@bpy.app.handlers.persistent
def clear(_):
Worker.JOB_WORK.clear()
while not Worker.JOB_CLEAR.empty():
try:
func = Worker.JOB_CLEAR.get()
func()
except Exception as e:
traceback.print_exc()
logger.error("%s: %s", type(e).__name__, e)
except KeyboardInterrupt:
...
def timer_reg():
Timer.reg()
Worker.reg()
bpy.app.handlers.load_pre.append(Worker.clear)
def timer_unreg():
Timer.unreg()
Worker.unreg()