def deferToThreadPool(reactor, threadpool, f, *args, **kwargs):
    "\n    Call the function C{f} using a thread from the given threadpool and return\n    the result as a Deferred.\n\n    This function is only used by client code which is maintaining its own\n    threadpool.  To run a function in the reactor's threadpool, use\n    C{deferToThread}.\n\n    @param reactor: The reactor in whose main thread the Deferred will be\n        invoked.\n\n    @param threadpool: An object which supports the C{callInThreadWithCallback}\n        method of C{twisted.python.threadpool.ThreadPool}.\n\n    @param f: The function to call.\n    @param *args: positional arguments to pass to f.\n    @param **kwargs: keyword arguments to pass to f.\n\n    @return: A Deferred which fires a callback with the result of f, or an\n        errback with a L{twisted.python.failure.Failure} if f throws an\n        exception.\n    "
    d = defer.Deferred()

    def onResult(success, result):
        if success:
            reactor.callFromThread(d.callback, result)
        else:
            reactor.callFromThread(d.errback, result)
    threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs)
    return d
Call the function C{f} using a thread from the given threadpool and return
the result as a Deferred.

This function is only used by client code which is maintaining its own
threadpool.  To run a function in the reactor's threadpool, use
C{deferToThread}.

@param reactor: The reactor in whose main thread the Deferred will be
    invoked.

@param threadpool: An object which supports the C{callInThreadWithCallback}
    method of C{twisted.python.threadpool.ThreadPool}.

@param f: The function to call.
@param *args: positional arguments to pass to f.
@param **kwargs: keyword arguments to pass to f.

@return: A Deferred which fires a callback with the result of f, or an
    errback with a L{twisted.python.failure.Failure} if f throws an
    exception.
def deferToThreadPool(reactor, threadpool, f, *args, **kwargs):
    "\n    Call the function C{f} using a thread from the given threadpool and return\n    the result as a Deferred.\n\n    This function is only used by client code which is maintaining its own\n    threadpool.  To run a function in the reactor's threadpool, use\n    C{deferToThread}.\n\n    @param reactor: The reactor in whose main thread the Deferred will be\n        invoked.\n\n    @param threadpool: An object which supports the C{callInThreadWithCallback}\n        method of C{twisted.python.threadpool.ThreadPool}.\n\n    @param f: The function to call.\n    @param *args: positional arguments to pass to f.\n    @param **kwargs: keyword arguments to pass to f.\n\n    @return: A Deferred which fires a callback with the result of f, or an\n        errback with a L{twisted.python.failure.Failure} if f throws an\n        exception.\n    "
    d = defer.Deferred()

    def onResult(success, result):
        if success:
            reactor.callFromThread(d.callback, result)
        else:
            reactor.callFromThread(d.errback, result)
    threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs)
    return d