-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadRunner.py
85 lines (65 loc) · 2.29 KB
/
ThreadRunner.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
#!/usr/bin/env python
# coding: utf-8
"""
ThreadRunner.
This module provides the class GtkThreadRunner that is used
to run a function on a thread and return the result when it's done
"""
import Queue
import threading
import traceback
import kaa
import time
from guicavane.Utils.Log import console
from StringIO import StringIO
class ThreadRunner(threading.Thread):
"""
Run `func` in a thread with `args` and `kwargs` as arguments, when
finished call callback with the result obtained or an exception caught.
"""
def __init__(self, callback, func, *args, **kwargs):
threading.Thread.__init__(self)
self.setDaemon(True)
self.callback = callback
self.func = func
self.args = args
self.kwargs = kwargs
self.result = Queue.Queue()
self.start()
self.check_thread = threading.Thread(target=self.check)
self.check_thread.start()
def run(self):
"""
Main function of the thread, run func with args and kwargs
and get the result, call callback with the result
if an exception is thrown call callback with the exception
"""
try:
result = (False, self.func())
except Exception, ex:
result = (True, ex)
if hasattr(self.func, "im_class"):
if self.func.im_class == type:
methodname = "%s.%s" % (self.func.__self__.__name__, \
self.func.im_func.func_name)
else:
methodname = "%s.%s" % (self.func.im_class.__name__, \
self.func.im_func.func_name)
else:
methodname = "%s" % self.func
error_str = StringIO()
error_str.write("running: %s, args: %s, kwargs: %s\nDetails:\n" % \
(methodname, self.args, self.kwargs))
traceback.print_exc(file=error_str)
error_str.seek(0)
print (error_str.read())
self.result.put(result)
def check(self):
""" Check if func finished. """
try:
result = self.result.get(True, 300)
except Queue.Empty:
return True
t = kaa.MainThreadCallback(self.callback, result)
t()
return False