forked from vadimcn/codelldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerthread.py
39 lines (31 loc) · 1000 Bytes
/
workerthread.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
import logging
import traceback
from threading import Thread
log = logging.getLogger('workerthread')
class ScopeGuard:
def __init__(self, target):
self.target = target
def __del__(self):
self.target()
# Thread with a built-in graceful shutdown flag
class WorkerThread(Thread):
def __init__(self, *args):
Thread.__init__(self, *args)
self.daemon = True
self.stopping = False
def start(self):
Thread.start(self)
# Automatically calls shutdown() when the returned token goes out of scope
return ScopeGuard(self.shutdown)
def shutdown(self):
log.info('%s thread is shutting down', self.__class__.__name__)
self.stopping = True
self.join()
log.info('%s thread has stopped', self.__class__.__name__)
def run(self):
try:
self.thread_proc()
except Exception as e:
log.error('%s', traceback.format_exc())
def thread_proc():
pass