-
Notifications
You must be signed in to change notification settings - Fork 6
/
nbmultitask.py
253 lines (204 loc) · 8.56 KB
/
nbmultitask.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from ipywidgets import Button, Layout
from threading import Thread
from multiprocessing import Process, Queue, Event
from logging.handlers import QueueHandler
from ipywidgets import Output
from IPython.display import display
from contextlib import contextmanager
from time import sleep
import sys
import random
from io import StringIO
import traceback
from IPython import get_ipython
from IPython.core.magic import Magics, magics_class, cell_magic
from logging import Handler
class LogToQueue(Handler):
def __init__(self,queue=None):
self.q = queue if queue else Queue()
Handler.__init__(self)
def write(self,x):
return self.q.put(x)
def put(self,x):
return self.q.put(x)
def get(self):
return self.q.get()
def empty(self):
return self.q.empty()
def print(self, x, end='\n'):
self.write(str(x) + end)
@contextmanager
def empty_context():
yield None
EmptyContext = empty_context()
def withLogAndControls(superclass):
class Processor(superclass):
def __init__(self, *init_args, loop=False, log_refresh_time=0.05, use_terminate=True, args=(), kwargs={}, **init_kwargs):
self.use_terminate = use_terminate
self.loop = loop
self.output = Output()
self.started = Event()
self.exiting = Event()
self.exited = Event()
self.errored = Event()
self.start_stop_button = Button(description='start')
self.start_stop_button.on_click(self.__toggle_start_stop__)
self.start_stop_button.button_style = 'success'
self.refresh_log_button = Button(description='refresh log')
self.refresh_log_button.on_click(lambda evt: self.refresh_log())
self.clear_log_button = Button(description='clear log')
self.clear_log_button.on_click(lambda evt: self.clear_log())
self.watch_log_button = Button(description='watch')
self.watch_log_button.button_style = 'primary'
self.watch_log_button.on_click(self.__toggle_watch_log__)
self.log = LogToQueue()
if issubclass(self.__class__,Thread):
kwargs['thread_print'] = self.log.print
self.log_refresh_time = log_refresh_time
self.watching_log = Event()
superclass.__init__(self,*init_args, args=args, kwargs=kwargs, **init_kwargs)
def refresh_log(self):
refreshed_once = False
while self.watching_log.is_set() or not refreshed_once:
refreshed_once = True
if self.exited.is_set():
self.watching_log.clear()
self.__disable_buttons_after_exited__()
while not self.log.empty():
msg = self.log.get()
with self.output:
if self.errored.is_set():
print(msg, end='', file=sys.stderr)
else:
print(msg, end='')
if self.exited.is_set() and self.log.empty():
return
sleep(self.log_refresh_time) # save a few CPU cycles
def watch(self):
self.watching_log.set()
self.watch_log_button.description = 'stop watching'
self.watch_log_button.button_style = 'warning'
# This thread will watch the log as long as self.watching_log is set.
# self.refresh_log()
Thread(target=self.refresh_log,name='watching log for %s' % str(self)).start()
def stop_watching(self):
self.watching_log.clear()
self.watch_log_button.description = 'watch'
self.watch_log_button.button_style = 'primary'
def clear_log(self,wait=False):
self.output.clear_output(wait=wait)
def show_log(self):
display(self.output)
def start(self):
super().start()
self.started.set()
with self.output:
print(self)
print('running...')
self.start_stop_button.description = 'stop'
self.start_stop_button.button_style = 'danger'
def stop(self):
if not self.started.is_set():
return
if self.errored.is_set():
with self.output:
while not self.log.q.empty():
msg = self.log.q.get()
print(msg, end='', file=sys.stderr)
if hasattr(self,'terminate') and self.use_terminate:
self.terminate()
with self.output:
print('terminating...')
self.exited.set()
else:
with self.output:
print('letting work finish before stopping...')
self.exiting.set()
self.exited.wait()
self.exiting.clear()
sleep(0.1) # Need to wait a little bit before the Process object's status is `stopped`
with self.output:
print(self)
self.__disable_buttons_after_exited__()
def run(self):
if issubclass(self.__class__,Process):
sys.stdout = self.log
sys.stderr = self.log
fn = self._target if self._target is not None else getattr(self,'work')
try:
if self.loop:
while not self.exiting.is_set():
fn(*self._args,**self._kwargs)
else:
fn(*self._args,**self._kwargs)
except Exception as e:
self.errored.set()
tb = StringIO()
traceback.print_exception(*sys.exc_info(), file=tb)
self.log.print(tb.getvalue())
# when the work is done, signal that we are finished
self.exited.set()
def __disable_buttons_after_exited__(self):
self.start_stop_button.description = 'finished'
self.start_stop_button.button_style = ''
self.start_stop_button.disabled = True
self.watch_log_button.close()
def __toggle_start_stop__(self,e=None):
if not self.started.is_set() and not self.exited.is_set():
self.start()
else:
self.stop()
def __toggle_watch_log__(self,e=None):
if not self.watching_log.is_set(): # and not self.exited.is_set():
self.watch()
elif not self.exited.is_set():
self.stop_watching()
elif self.exited.is_set():
self.stop_watching()
def show_start_stop_buttons(self):
display(self.start_stop_button)
def show_log_buttons(self):
display(self.watch_log_button)
display(self.clear_log_button)
def control_panel(self):
self.show_start_stop_buttons()
self.show_log_buttons()
self.show_log()
return Processor
ProcessWithLogAndControls = withLogAndControls(Process)
ThreadWithLogAndControls = withLogAndControls(Thread)
# https://ipython.readthedocs.io/en/stable/config/custommagics.html
# https://ipython-books.github.io/14-creating-an-ipython-extension-with-custom-magic-commands/
# The class MUST call this class decorator at creation time
@magics_class
class RunAsync(Magics):
@cell_magic
def thread(self, line, cell):
def fn(thread_print):
ipy = get_ipython()
ipy.push({'thread_print': thread_print})
ipy.run_cell(cell)
# errors in thread should propagate to interactive shell
task = ThreadWithLogAndControls(target=fn, name='nbmultitask-thread')
return task.control_panel()
@cell_magic
def process(self, line, cell):
def fn():
ipy = get_ipython()
result = ipy.run_cell(cell)
# send errors to parent
if result.error_before_exec:
raise result.error_before_exec
if result.error_in_exec:
raise result.error_in_exec
task = ProcessWithLogAndControls(target=fn, name='nbmultitask-process')
return task.control_panel()
def load_ipython_extension(ipython):
"""
Any module file that define a function named `load_ipython_extension`
can be loaded via `%load_ext module.path` or be configured to be
autoloaded by IPython at startup time.
"""
# You can register the class itself without instantiating it. IPython will
# call the default constructor on it.
ipython.register_magics(RunAsync)