|
26 |
|
26 |
|
27 |
import functools |
27 |
import functools |
28 |
import sys |
28 |
import sys |
29 |
import thread |
29 |
import threading |
30 |
import traceback |
30 |
import traceback |
31 |
|
31 |
|
32 |
__all__ = [ 'Simple' ] |
32 |
__all__ = [ 'Simple' ] |
Lines 59-66
def __init__( self, name, function, callback ):
|
Link Here
|
---|
|
59 |
self._trace = None |
59 |
self._trace = None |
60 |
self._exc_info = None |
60 |
self._exc_info = None |
61 |
self._finished = False |
61 |
self._finished = False |
62 |
self._id = None |
62 |
self._thread = threading.Thread(target=self._run, name=self._name) |
63 |
self._lock = thread.allocate_lock() |
63 |
self._lock = threading.Lock() |
64 |
global _threads |
64 |
global _threads |
65 |
if not _threads: |
65 |
if not _threads: |
66 |
notifier.dispatcher_add( _simple_threads_dispatcher ) |
66 |
notifier.dispatcher_add( _simple_threads_dispatcher ) |
|
72 |
|
72 |
|
73 |
def run( self ): |
73 |
def run( self ): |
74 |
"""Starts the thread""" |
74 |
"""Starts the thread""" |
75 |
self._id = thread.start_new_thread( self._run, () ) |
75 |
self._thread.start() |
76 |
|
76 |
|
77 |
def _run( self ): |
77 |
def _run( self ): |
78 |
"""Encapsulates the given thread function to handle the return |
78 |
"""Encapsulates the given thread function to handle the return |
79 |
value in a thread-safe way and to catch exceptions raised from |
79 |
value in a thread-safe way and to catch exceptions raised from |
80 |
within it.""" |
80 |
within it.""" |
81 |
try: |
81 |
try: |
82 |
tmp = self._function() |
82 |
result = self._function() |
83 |
trace = None |
83 |
trace = None |
84 |
exc_info = None |
84 |
exc_info = None |
85 |
except BaseException, e: |
85 |
except BaseException as exc: |
86 |
exc_info = sys.exc_info() |
86 |
exc_info = sys.exc_info() |
87 |
trace = traceback.format_tb( sys.exc_info()[ 2 ] ) |
87 |
trace = traceback.format_tb( sys.exc_info()[ 2 ] ) |
88 |
tmp = e |
88 |
result = exc |
89 |
self._lock.acquire() |
89 |
self.lock() |
90 |
self._result = tmp |
90 |
try: |
91 |
self._trace = trace |
91 |
self._result = result |
92 |
self._exc_info = exc_info |
92 |
self._trace = trace |
93 |
self._finished = True |
93 |
self._exc_info = exc_info |
94 |
self._lock.release() |
94 |
self._finished = True |
|
|
95 |
finally: |
96 |
self.unlock() |
95 |
|
97 |
|
96 |
@property |
98 |
@property |
97 |
def result( self ): |
99 |
def result( self ): |
Lines 150-163
def _simple_threads_dispatcher():
|
Link Here
|
---|
|
150 |
|
152 |
|
151 |
for task in _threads[ : ]: |
153 |
for task in _threads[ : ]: |
152 |
task.lock() |
154 |
task.lock() |
153 |
if task.finished: |
155 |
try: |
154 |
task.announce() |
156 |
if task.finished: |
155 |
_threads.remove( task ) |
157 |
task.announce() |
156 |
elif hasattr( task, '_signals' ): |
158 |
_threads.remove( task ) |
157 |
for signal, args in task._signals: |
159 |
elif hasattr( task, '_signals' ): |
158 |
task.signal_emit( signal, *args ) |
160 |
for signal, args in task._signals: |
159 |
task._signals = [] |
161 |
task.signal_emit( signal, *args ) |
160 |
task.unlock() |
162 |
task._signals = [] |
|
|
163 |
finally: |
164 |
task.unlock() |
161 |
|
165 |
|
162 |
return len( _threads ) > 0 |
166 |
return len( _threads ) > 0 |
163 |
|
167 |
|