def call_in_thread(self, callback, errback, f, *args, **kwargs):
"""
Execute a callable object in a new separate thread.
@param callback: A function to call in case C{f} was successful, it
will be passed the return value of C{f}.
@param errback: A function to call in case C{f} raised an exception,
it will be pass a C{(type, value, traceback)} tuple giving
information about the raised exception (see L{sys.exc_info}).
@note: Both C{callback} and C{errback} will be executed in the
the parent thread.
"""
def on_success(result):
if callback:
return callback(result)
def on_failure(failure):
exc_info = (failure.type, failure.value, failure.tb)
if errback:
errback(*exc_info)
else:
logging.error(exc_info[1], exc_info=exc_info)
deferred = deferToThread(f, *args, **kwargs)
deferred.addCallback(on_success)
deferred.addErrback(on_failure)
评论列表
文章目录